【问题标题】:Reading two files simultaneously conflicts buffer同时读取两个文件冲突缓冲区
【发布时间】:2017-07-12 22:05:57
【问题描述】:

请我尝试读取两个文件以进行比较,但是按部分进行比较,所以我同时读取两个文件,并且在每个部分之后我都在执行比较代码,但是第二个文件的缓冲读取器会递增每次更改部分时。

这是我正在使用的代码

    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) {

        }
    }
}
}

每次调用第一个 while 循环并执行 in.readline() 时,读取器 in2 都会跳过一行。

缓冲区是否有任何特殊性或可以创建任何冲突?

【问题讨论】:

  • 没有这样的冲突,建议问题出在你的逻辑上,你在某个地方读了两遍。例如,在`inN = new BufferedReader(new FileReader(filePath1));`之后in2N.readLine();有什么用?
  • 会不会是因为`command=false`?
  • 不,我不这么认为为什么缓冲区会受到影响,我做了一些测试,缓冲区 in2 在第一个 while 循环中直接在 in.readline() 之后跳过了一行。

标签: java file while-loop buffer bufferedreader


【解决方案1】:

是否有任何缓冲区的特殊性或可以创建的任何冲突?

没有。 BufferedReaders 工作。

  • 当你有不同的 BufferedReader 从不同的文件中读取时,不会有“冲突”。

  • 当你有不同的 BufferedReader 从不同的 FileReader 读取同一个文件时,不会有“冲突”。在这种情况下,您会从同一个地方获得两个独立的字符(或行)“流”。它们不会/不能相互干扰。

可能发生冲突的情况是(例如):

  • 当您使用两个不同的 BufferedReader 包装相同的底层 Reader 时,它们将“看到”文件的交替块。
  • 当您有两段代码无意中共享同一个 BufferedReader 时,代码将看到不同的部分(取决于读取的粒度等)。
  • 当从包装的 Reader 读取其他内容时,BufferedReader 会遇到部分缺失。
  • 当同时读取和写入底层文件(例如另一个程序)时,行为将不可预测1

我没有看过你的代码。它看起来很复杂,而且我没有时间对 预期的 逻辑进行逆向工程。但是,问题将出在该逻辑中。

(提示:我不会尝试通过并行读取每个文件两次来实现这种事情......)


我做了一些测试,缓冲区 in2 在第一个 while 循环中的 in.readline() 之后直接跳过了一行。

毫无疑问,您要么测试不正确,要么误解了证据的含义。但是我们看不到测试是什么或结果是什么。


1 - 这是否可能取决于本机操作系统的文件锁定行为。默认情况下,Windows 会锁定文件以防止这种情况发生,但 Linux 不会。

【讨论】:

  • 非常感谢您的澄清,基本上是的,第二次出现逻辑问题,而我需要在阅读该行之前检查布尔值。非常感谢。
【解决方案2】:

谢谢大家,

我解决了它,正如你们所有人提到的,第二个 while 循环中存在逻辑问题,循环读取该行,然后检查 bool 是真还是假,所以我将其反转,所以首先检查 bool 并那么当 bool 处于 false 状态时,不会读取该行。

所以不要阅读,然后检查

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

我反转了条件,因此检查了“命令”变量,然后将读取该行

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

非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    相关资源
    最近更新 更多