Search code examples
javafilewhile-loopbufferbufferedreader

Reading two files simultaneously conflicts buffer


Please I'm trying to read two files to do a comparison but by section, so I'm reading simultaneaously two files and after each section I'm executing the comparison code, but the buffered reader for the second file is incremented every time the section is changed.

Here is the code I'm using

    static public void compare (String filePath1,String filePath2){
    PrintStream console=System.out;
    BufferedReader in = null;   
    BufferedReader inN=null;
    BufferedReader in2=null;
    BufferedReader in2N=null;
    String line = "", line2= "";
    boolean command=true;
    try {   

        in = new BufferedReader(new FileReader(filePath1));
        inN = new BufferedReader(new FileReader(filePath1));
        inN.readLine();
        File file = new File("C:/Users/Dell/Desktop/command1.txt");        
         PrintStream printStreamToFile = new PrintStream(file);

         in2= new BufferedReader(new FileReader(filePath2));

         in2N= new BufferedReader(new FileReader(filePath2));
         in2N.readLine();
         File file1 = new File("C:/Users/Dell/Desktop/command2.txt");        
         PrintStream printStreamToFile1 = new PrintStream(file1);

        while ((line = in.readLine()) != null ) {
                String next=inN.readLine();
                System.setOut(console);
                System.out.println("print in 1"+line+" my next is:"+next);
                System.setOut(printStreamToFile);
                System.out.println(line);
            if(next != null && next.contains("#")){


                 command=true;
                    while ((line2 = in2.readLine()) != null && command == true ) {


                        String next2=in2N.readLine();
                        System.setOut(console);
                        System.out.println("print in 2"+line2+" my next is: "+next2);
                        System.setOut(printStreamToFile1);
                        System.out.println(line2);
                        if(next2 != null && next2.contains("#")){
                            System.setOut(console);
                            System.out.println("I m comparing");

                            List<String> original = fileToLines(file.getPath());
                            List<String> revised  = fileToLines(file1.getPath());
                            File fileDiff = new File("C:/Users/Dell/Desktop/commandDiff.txt");
                            PrintStream printStreamToFileDiff = new PrintStream(fileDiff);  
                            System.setOut(printStreamToFileDiff);
                            // Compute diff. Get the Patch object. Patch is the container for computed deltas.
                            Patch<String> patch = DiffUtils.diff(original, revised);

                            for (Delta<String> delta: patch.getDeltas()) {
                                System.out.println(delta);
                            }

                            command=false;
                        }
                    }   

            }


        }   




} catch (IOException e) {
    e.printStackTrace();
} finally {

    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {

        }
    }
    if (in2 != null) {
        try {
            in2.close();
        } catch (IOException e) {

        }
    }
}
}

Every time the first while loop is called and in.readline() executed, the reader in2 is skipping one line.

Is there any specificity for buffers or any conflicts that can be created?


Solution

  • Is there any specificity for buffers or any conflicts that can be created?

    No. BufferedReaders work.

    • When you have different BufferedReaders that are reading from different files, there will be no "conflict".

    • When you have different BufferedReaders reading from different FileReaders for the same file, there will be no "conflict". In that scenario, you get two independent "streams" of characters (or lines) coming from the same place. They do not / cannot interfere with each other.

    Cases where you could get conflict are (for example):

    • When you wrap the same underlying Reader with two different BufferedReaders, they will "see" alternating blocks of the file.
    • When you have two bits of code unwittingly sharing the same BufferedReader, the code will see different parts (depending on the granularity of reads, etc).
    • When something else is reading from the wrapped Reader, the BufferedReader will experience parts missing.
    • When the underlying file is being simultaneously read and written (e.g. another program) behavior will be unpredictable1.

    I haven't looked at your code. It looks complicated, and I don't have the time to reverse engineer the intended logic. However, the problem will be something in that logic.

    (Hint: I wouldn't try to implement this kind of thing by reading each file two times in parallel ...)


    I made some tests the buffer in2 is skipping a line directly after the in.readline() in the first while loop.

    There is little doubt in my mind that you have either tested incorrectly, or misinterpreted what the evidence is saying. But we can't see either what the tests were or what the results were.


    1 - Whether this is possible depends on the native OS's file locking behavior. By default, Windows locks files to prevent this, but Linux doesn't.