请我尝试阅读两个文件进行比较但是按部分进行比较,所以我同时阅读两个文件,每个部分后我都在执行比较代码,但是第二个文件的缓冲读取器增加了每次更改部分。
这是我正在使用的代码
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正在跳过一行。
缓冲区是否有任何特异性或可以创建的任何冲突?
答案 0 :(得分:1)
缓冲区是否有任何特异性或可以创建的任何冲突?
没有。 BufferedReaders工作。
如果您有不同的BufferedReader从不同的文件中读取,则不会发生冲突&#34;。
当您从同一文件的不同FileReader读取不同的BufferedReaders时,将不存在&#34;冲突&#34;。在这种情况下,您将获得两个独立的&#34;流&#34;字符(或行)来自同一个地方。他们没有/不能互相干涉。
可能发生冲突的情况(例如):
我还没看过你的代码。它看起来很复杂,我没有时间对预期的逻辑进行逆向工程。但是,问题将出现在那个逻辑中。
(提示:我不会尝试通过两次并行读取每个文件来实现这种事情......)
我做了一些测试,缓冲区in2正在第一个while循环中的in.readline()之后直接跳过一行。
我无疑怀疑你是否测试不当,或误解了证据所说的内容。但我们无法看到测试的内容或结果是什么。
1 - 这是否可行取决于本机操作系统的文件锁定行为。默认情况下,Windows锁定文件以防止这种情况,但Linux不会。
答案 1 :(得分:0)
谢谢大家,
我解决了它,正如大家所提到的那样,在第二个while循环中存在一个逻辑问题,循环读取该行然后检查bool是否为真或假,所以我反转了所以首先检查bool并且当bool处于假状态时,线路不会被重新命名。
因此,而不是阅读和检查后
while ((line2 = in2.readLine()) != null && command == true )
我反转条件所以检查“命令”var然后读取行
while (command == true && (line2 = in2.readLine()) != null)
非常感谢。