Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同时读取两个文件会冲突缓冲区_Java_File_While Loop_Buffer_Bufferedreader - Fatal编程技术网

Java 同时读取两个文件会冲突缓冲区

Java 同时读取两个文件会冲突缓冲区,java,file,while-loop,buffer,bufferedreader,Java,File,While Loop,Buffer,Bufferedreader,请注意,我试图读取两个文件来进行比较,但是要按节进行,所以我同时读取两个文件,在每个节之后,我执行比较代码,但是第二个文件的缓冲读取器在每次节更改时都会增加 这是我正在使用的代码 static public void compare (String filePath1,String filePath2){ PrintStream console=System.out; BufferedReader in = null; BufferedReader inN=

请注意,我试图读取两个文件来进行比较,但是要按节进行,所以我同时读取两个文件,在每个节之后,我执行比较代码,但是第二个文件的缓冲读取器在每次节更改时都会增加

这是我正在使用的代码

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

        }
    }
}
}
静态公共void比较(字符串filePath1、字符串filePath2){
PrintStream控制台=System.out;
BufferedReader in=null;
BufferedReader inN=null;
BufferedReader in2=null;
BufferedReader in2N=null;
字符串行=”,第2行=”;
布尔命令=true;
试试{
in=新的BufferedReader(新的文件读取器(文件路径1));
inN=newbufferedreader(newfilereader(filePath1));
readLine()客栈;
File File=新文件(“C:/Users/Dell/Desktop/command1.txt”);
PrintStream printStreamToFile=新的打印流(文件);
in2=新的BufferedReader(新的文件读取器(filePath2));
in2N=newbufferedreader(newfilereader(filePath2));
in2N.readLine();
File file1=新文件(“C:/Users/Dell/Desktop/command2.txt”);
PrintStream printStreamToFile1=新的PrintStream(文件1);
而((line=in.readLine())!=null){
字符串next=inN.readLine();
系统放样(控制台);
System.out.println(“打印输入1”+行+”我的下一行是:“+下一行”);
系统放样(printStreamToFile);
系统输出打印项次(行);
if(next!=null&&next.contains(“#”){
命令=真;
while((line2=in2.readLine())!=null&&command==true){
字符串next2=in2N.readLine();
系统放样(控制台);
System.out.println(“打印2”+line2+”我的下一个是:“+next2”);
系统放样(打印流文件1);
系统输出打印项次(第2行);
if(next2!=null&&next2.contains(“#”){
系统放样(控制台);
System.out.println(“我正在比较”);
List original=filetoline(file.getPath());
List revised=filetoline(file1.getPath());
File fileDiff=新文件(“C:/Users/Dell/Desktop/commandDiff.txt”);
PrintStream printStreamToFileDiff=新的打印流(fileDiff);
系统放样(printStreamToFileDiff);
//计算差异。获取面片对象。面片是计算增量的容器。
Patch Patch=DiffUtils.diff(原件,修订版);
对于(增量增量:patch.getDeltas()){
系统输出打印项数(增量);
}
命令=假;
}
}   
}
}   
}捕获(IOE异常){
e、 printStackTrace();
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){
}
}
如果(in2!=null){
试一试{
in2.close();
}捕获(IOE异常){
}
}
}
}
每次调用第一个while循环并在.readline()中执行时,读取器in2都会跳过一行

缓冲区或可能产生的冲突是否有任何特殊性

缓冲区或可能产生的冲突是否有任何特殊性

不,BufferedReader可以工作

  • 当您有从不同文件读取的不同BufferedReader时,将不会有“冲突”

  • 当不同的BufferedReader从不同的文件读取器读取同一个文件时,将不会出现“冲突”。在该场景中,您将获得来自同一位置的两个独立的字符(或行)流。它们不会/不能相互干扰

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

  • 当您使用两个不同的BufferedReader包装相同的底层读取器时,它们将“看到”文件的交替块
  • 当两位代码无意中共享同一个BufferedReader时,代码将看到不同的部分(取决于读取的粒度等)
  • 当从包装读取器读取其他内容时,BufferedReader将遇到缺少部分的情况
  • 当同时读取和写入基础文件(例如,另一个程序)时,行为将是不可预测的1
我还没看过你的代码。它看起来很复杂,我没有时间对预期的逻辑进行反向工程。然而,问题就在于这种逻辑

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


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

毫无疑问,在我看来,你要么测试错误,要么曲解了证据所说的话。但我们既看不到测试结果是什么,也看不到结果是什么


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

谢谢大家

我解决了它,正如大家提到的,在第二个while循环中有一个逻辑问题,循环读取行,然后检查bool是否为真,所以我反转了,所以bool首先被检查,然后当bool为假状态时,行不被读取

因此,与其阅读,还不如检查一下

while ((line2 = in2.readLine()) != null && command == true ) 
我反转了条件,因此检查了“命令”变量,然后将读取该行

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

非常感谢。

没有这种冲突,请提出问题