同时读取两个文件冲突缓冲区
Reading two files simultaneously conflicts buffer
拜托,我正在尝试读取两个文件来进行比较,但是是按部分进行的,所以我同时读取了两个文件,并且在每个部分之后我正在执行比较代码,但是缓冲 reader每次更改该部分时,第二个文件都会递增。
这是我正在使用的代码
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() 时,reader in2 都会跳过一行。
缓冲区是否有任何特殊性或可能产生的冲突?
Is there any specificity for buffers or any conflicts that can be created?
没有。缓冲Readers 工作。
当你有不同的 BufferedReader 从不同的文件读取时,将不会有 "conflict".
当你有不同的 BufferedReaders 从不同的 FileReaders 读取同一个文件时,将不会有 "conflict"。在那种情况下,您会从同一个地方获得两个独立的 "streams" 字符(或行)。他们不会/不能互相干扰。
您可能发生冲突的情况是(例如):
- 当您用两个不同的 BufferedReader 包装相同的底层 Reader 时,它们将 "see" 文件的交替块。
- 当您有两位代码 无意中 共享相同的 BufferedReader 时,代码将看到不同的部分(取决于读取的粒度等)。
- 当其他东西从包装的 Reader 中读取时,BufferedReader 将遇到部分缺失。
- 当同时读取和写入底层文件(例如另一个程序)时,行为将不可预测1.
我没有看过你的代码。它看起来很复杂,而且我没有时间对 intended 逻辑进行逆向工程。但是,问题将出在该逻辑中。
(提示:我不会尝试通过并行读取每个文件两次来实现这种事情...)
I made some tests the buffer in2 is skipping a line directly after the in.readline() in the first while loop.
我认为您要么测试不正确,要么误解了证据的含义。但是我们既看不到测试是什么,也看不到结果。
1 - 这是否可行取决于本机 OS 的文件锁定行为。默认情况下,Windows 会锁定文件以防止出现这种情况,但 Linux 不会。
谢谢大家,
我解决了,正如你们所有人提到的那样,在第二个 while 循环中存在逻辑问题,循环读取该行然后检查 bool 是真还是假,所以我将其反转以便检查 bool first 和 then 当 bool 处于 false 状态时,不会读取该行。
所以不要阅读然后检查
while ((line2 = in2.readLine()) != null && command == true )
我反转了条件,所以 "command" var 被检查然后行将被读取
while (command == true && (line2 = in2.readLine()) != null)
非常感谢。
拜托,我正在尝试读取两个文件来进行比较,但是是按部分进行的,所以我同时读取了两个文件,并且在每个部分之后我正在执行比较代码,但是缓冲 reader每次更改该部分时,第二个文件都会递增。
这是我正在使用的代码
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() 时,reader in2 都会跳过一行。
缓冲区是否有任何特殊性或可能产生的冲突?
Is there any specificity for buffers or any conflicts that can be created?
没有。缓冲Readers 工作。
当你有不同的 BufferedReader 从不同的文件读取时,将不会有 "conflict".
当你有不同的 BufferedReaders 从不同的 FileReaders 读取同一个文件时,将不会有 "conflict"。在那种情况下,您会从同一个地方获得两个独立的 "streams" 字符(或行)。他们不会/不能互相干扰。
您可能发生冲突的情况是(例如):
- 当您用两个不同的 BufferedReader 包装相同的底层 Reader 时,它们将 "see" 文件的交替块。
- 当您有两位代码 无意中 共享相同的 BufferedReader 时,代码将看到不同的部分(取决于读取的粒度等)。
- 当其他东西从包装的 Reader 中读取时,BufferedReader 将遇到部分缺失。
- 当同时读取和写入底层文件(例如另一个程序)时,行为将不可预测1.
我没有看过你的代码。它看起来很复杂,而且我没有时间对 intended 逻辑进行逆向工程。但是,问题将出在该逻辑中。
(提示:我不会尝试通过并行读取每个文件两次来实现这种事情...)
I made some tests the buffer in2 is skipping a line directly after the in.readline() in the first while loop.
我认为您要么测试不正确,要么误解了证据的含义。但是我们既看不到测试是什么,也看不到结果。
1 - 这是否可行取决于本机 OS 的文件锁定行为。默认情况下,Windows 会锁定文件以防止出现这种情况,但 Linux 不会。
谢谢大家,
我解决了,正如你们所有人提到的那样,在第二个 while 循环中存在逻辑问题,循环读取该行然后检查 bool 是真还是假,所以我将其反转以便检查 bool first 和 then 当 bool 处于 false 状态时,不会读取该行。
所以不要阅读然后检查
while ((line2 = in2.readLine()) != null && command == true )
我反转了条件,所以 "command" var 被检查然后行将被读取
while (command == true && (line2 = in2.readLine()) != null)
非常感谢。