2

I have two text files and I want to compare the contents of text files using java.

For example First file e1.txt has the content "hello this is india" , and another e2.txt contents "hello this is usa". I want the output should be the text which is not similer in both files(here output should be india or usa).

Problem which I was facing here is that java IO method reads line by line so in this case no output will be given(both lines are different), also white spaces should be ignored. I will be very much thankful if anyone will help me in this problem.

Here is my Code :

 public void fh() throws FileNotFoundException, IOException{
    File f1=new File("C:\\\\Users\\\\Ramveer\\\\Desktop\\\\idrbt Project\\\\e1.txt");
    File f2=new File("C:\\\\Users\\\\Ramveer\\\\Desktop\\\\idrbt Project\\\\e2.txt");
    FileInputStream fi1=new FileInputStream(f1);
    FileInputStream fi2=new FileInputStream(f2); 
    DataInputStream di1=new DataInputStream(fi1);
    BufferedReader br1=new BufferedReader(new InputStreamReader(di1));
    DataInputStream di2=new DataInputStream(fi2);
    BufferedReader br2=new BufferedReader(new InputStreamReader(di2));
    String s1, s2;  
    while ((s1=br1.readLine())!=null && (s2=br2.toString())!=null) 
     {
    if(!s1.equals(s2)){
    System.out.println(s1);
      }
    } 
}
15
  • Please format your code :) Commented May 27, 2013 at 11:51
  • does word order matter? i mean should it return the same result for comparing "hello this is india" to "hello this is usa" and "hello india this is" to "hello this is usa"? Commented May 27, 2013 at 11:56
  • You should learn how to debug. Insert a breakpoint and step through the lines. Watch your variables and see what value they have. You'll find your problem and be able to solve it soon enough. Commented May 27, 2013 at 11:56
  • 1
    Try java.util.Scanner Commented May 27, 2013 at 11:57
  • 1
    You are invoking toString on the second reader. Probably not what you want. Commented May 27, 2013 at 12:00

4 Answers 4

3

Just as what I commented, use java.util.Scanner

public static void fha(InputStream is1, InputStream is2) throws IOException {
    Scanner sc1 = new Scanner(is1);
    Scanner sc2 = new Scanner(is2);
    while (sc1.hasNext() && sc2.hasNext()) {
        String str1 = sc1.next();
        String str2 = sc2.next();
        if (!str1.equals(str2))
            System.out.println(str1 + " != " + str2);
    }
    while (sc1.hasNext())
        System.out.println(sc1.next() + " != EOF");
    while (sc2.hasNext())
        System.out.println("EOF != " + sc2.next());
    sc1.close();
    sc2.close();
}
Sign up to request clarification or add additional context in comments.

Comments

2

Take a look at the StringTokenizer class, and the String.indexOf(String s) method.

You can use a StringTokenizer to break-up a String into sections, separated by delimiters.

You can use String.indexOf(String s) to find a particular String within another String.

You can probably solve your problem using a combination of these.

Comments

2

It depends on how you want the comparison go (e.g. word by word, char by char), i.e. given

 Hello this is India

and

 Hello this is Indonesia

should it output:

  1. "India"/"Indonesia"?
  2. "ia" vs. "onesia"?

In any case, you can use br1.read() and br2.read() to do a character by character comparison (case 2). Or you could use loops to read each file until the next delimiter (space maybe) and then compare the words.

2 Comments

comparison is word by word.these are simple lines ,we can use loop until next space, but what else if i have many lines in text files and how would you determine the no of words.
a line end could also be treated as end of word (this is common). So read word loouped until char is ' ' or '\n' or '\r'. Do this on each file and then output different words.
-1
import java.io.*;

public class CompareTextFiles {

    public static void main(String args[]) throws Exception {

      FileInputStream fstream1 = new FileInputStream("C:\\text1.txt");
      FileInputStream fstream2 = new FileInputStream("C:\\text2.txt");

      DataInputStream in1= new DataInputStream(fstream1);
      DataInputStream in2= new DataInputStream(fstream2);

      BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
      BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));

      String strLine1, strLine2;


      while((strLine1 = br1.readLine()) != null && (strLine2 = br2.readLine()) != null){
          if(strLine1.equals(strLine2)){
              System.out.println(strLine1);

          }
      }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.