The Artima Developer Community
Sponsored Link

Java Answers Forum
File Reading

2 replies on 1 page. Most recent reply: Jul 6, 2012 11:51 PM by nikhil kadam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Alan Franco

Posts: 1
Nickname: avfm
Registered: Dec, 2010

File Reading Posted: Dec 8, 2010 11:15 PM
Reply to this message Reply
Advertisement
Can somebody explain to me how this code works. It's supposed to compare a text file line by line, but I'm not sure if it compares one file or two files. also, when I run it, it doesn't show any compiling errors, but it doesn't do anything.

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.util.List;


public class Diff
{
public static void readersAsText(Reader r1, String name1, Reader r2, String name2,
List diffs)
throws IOException
{
LineNumberReader reader1 = new LineNumberReader(r1);
LineNumberReader reader2 = new LineNumberReader(r2);
String line1 = reader1.readLine();
String line2 = reader2.readLine();
while (line1 != null && line2 != null)
{
if (!line1.equals(line2))
{
diffs.add("File \"" + name1 + "\" and file \"" +
name2 + "\" differ at line " + reader1.getLineNumber() +
":" + "\n" + line1 + "\n" + line2);
break;
}
line1 = reader1.readLine();
line2 = reader2.readLine();
}
if (line1 == null && line2 != null)
diffs.add("File \"" + name2 + "\" has extra lines at line " +
reader2.getLineNumber() + ":\n" + line2);
if (line1 != null && line2 == null)
diffs.add("File \"" + name1 + "\" has extra lines at line " +
reader1.getLineNumber() + ":\n" + line1);
}
}


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: File Reading Posted: Dec 9, 2010 12:26 AM
Reply to this message Reply
Given that the class contains no "main" method, how are you running it?

nikhil kadam

Posts: 1
Nickname: nikalldway
Registered: Jul, 2012

Re: File Reading Posted: Jul 6, 2012 11:51 PM
Reply to this message Reply
While taking the input from user u can write in file and print it at the same time u can read it from the file this is something i tried it .


public static void main(String[] args)throws Exception{
FileWriter fw = new FileWriter("C:\\batches\\helloo.txt);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw,true);
ArrayList<String> S = new ArrayList<>();
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println("enter value");
String a = br.readLine();
while(!a.equals("end"))
{
S.add(a);
a=br.readLine();
}
System.out.println("printing value");
for(String v : S)
{
System.out.println(v);
pw.println(v);
}
}

FileReader fr = new FileReader("C:\\batches\\helloo.txt");
BufferedReader br = new BufferedReader(fr);
String s= br.readLine();
while(s!=null)
{
System.out.println(s);
s=br.readLine();
}

Flat View: This topic has 2 replies on 1 page
Topic: BOOK Previous Topic   Next Topic Topic: grep in java ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use