本文介绍了比较两个文件在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图比较两个.txt文件(即它们的内容),但是当我执行这个代码时,我的应用程序进入了一个无限循环。为什么?$ p $
public int compareFile(String fILE_ONE2,String fILE_TWO2)throws Exception
{
文件f1 =新文件(fILE_ONE2); // OUTFILE
文件f2 =新文件(fILE_TWO2); // INPUT
FileReader fR1 =新的FileReader(f1);
FileReader fR2 =新的FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag = 1; ((flag = 1)&((line1 = reader1.readLine())!= null)&&((line2 = reader2.readLine())!= null))
{
if(!line1.equalsIgnoreCase(line2))
flag = 0;
else
flag = 1;
}
reader1.close();
reader2.close();
返回标志;
解决方案
我假设您正在比较两个小尺寸文本文件。
import java.io. *;
$ b $ public class Diff {
public static void main(String [] args)抛出FileNotFoundException,IOException {
文件f1 = new File(args [0]) ; //输出
文件f2 =新文件(args [1]); // INPUT
FileReader fR1 =新FileReader(f1);
FileReader fR2 =新的FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag = 1; ((flag = 1)&((line1 = reader1.readLine())!= null)
&&((line2 = reader2.readLine())!= null)){
if(!line1.equalsIgnoreCase(line2))
flag = 0;
}
reader1.close();
reader2.close();
System.out.println(Flag+ flag);
$ b我在2个不同的文本文件上运行它。这是输出。 javac Diff.java&& java Diff a.txt b.txt
Flag 0
如果你认为你有一个无限的循环,问题可能在其他地方。
I am trying to compare two .txt files (i.e their contents), but when I execute this code my application goes into an infinite loop. Why?
public int compareFile(String fILE_ONE2, String fILE_TWO2)throws Exception
{
File f1 = new File(fILE_ONE2); //OUTFILE
File f2 = new File(fILE_TWO2); //INPUT
FileReader fR1 = new FileReader(f1);
FileReader fR2 = new FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag=1;
while ((flag==1) &&((line1 = reader1.readLine()) != null)&&((line2 = reader2.readLine()) != null))
{
if (!line1.equalsIgnoreCase(line2))
flag=0;
else
flag=1;
}
reader1.close();
reader2.close();
return flag;
}
解决方案 I converted your code into a main program. There is no infinite loop in this code.
I am assuming you are comparing 2 text files of a small-ish size.
import java.io.*;
public class Diff {
public static void main(String[] args) throws FileNotFoundException, IOException {
File f1 = new File(args[0]);// OUTFILE
File f2 = new File(args[1]);// INPUT
FileReader fR1 = new FileReader(f1);
FileReader fR2 = new FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag = 1;
while ((flag == 1) && ((line1 = reader1.readLine()) != null)
&& ((line2 = reader2.readLine()) != null)) {
if (!line1.equalsIgnoreCase(line2))
flag = 0;
}
reader1.close();
reader2.close();
System.out.println("Flag " + flag);
}
}
I ran it on 2 small different text files. This is the output.
javac Diff.java && java Diff a.txt b.txt
Flag 0
If you think you have an infinite loop, the issue might be elsewhere.
这篇关于比较两个文件在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!