I am trying to compare the contents of a file with a String object. But even though the contents are same, it is telling that the contents are different and adding the same contents again and again. For every run of this code, same content is rewritten which is not what I want. Same content should not be rewritten.
我试图比较文件的内容与String对象。但即使内容相同,也要说内容不同,并一次又一次地添加相同的内容。对于此代码的每次运行,重写相同的内容,这不是我想要的。不应重写相同的内容。
package fileOperations;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class CompareFiles {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
CompareFiles cf = new CompareFiles();
String file1 =("new apple") ;
System.out.println(file1);
System.out.println("length"+file1.length());
File f = new File("C:\\Documents and Settings\\newfile.txt");
cf.compareFiles(file1, f);
}
private void compareFiles(String new_content,File f) throws IOException{
StringBuffer old_content=new StringBuffer();
String str;
FileInputStream fstream = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
while ((str = br.readLine()) != null) {
System.out.println(str);
old_content.append(str);
}
fstream.close();
System.out.println("length "+old_content.length());
if(!(0==new_content.compareTo(old_content.toString()))){
createPrivateKey(f, new_content);
}
}
public void createPrivateKey(File privateKeyFile,String keyString ){
try {
FileWriter fs = new FileWriter(privateKeyFile);
fs.write(keyString);
fs.close();
System.out.println("Private key file contents added");
} catch (IOException e) {
System.out.println("Unable to access private key file and/or config file");
}
}
}
2 个解决方案
#1
0
I would have avoided trying to re-invent the wheel and use Apache commons - FileUtils.contentEquals()
to compare two files and FileUtils.readFileToString()
to get it as a String
object
我本来避免尝试重新发明*并使用Apache commons - FileUtils.contentEquals()来比较两个文件和FileUtils.readFileToString()来将它作为String对象
Apache commons is an open source widely used library with very premissive license, so using it is usually a good choice.
Apache commons是一个开源广泛使用的库,具有非常优先的许可证,因此使用它通常是一个不错的选择。
P.S. Your bug might be that you are not appending the line-terminator after each line, but it is going to be hard to tell without knowing the contents.
附:您的错误可能是您没有在每行之后附加行终止符,但在不知道内容的情况下很难分辨。
#2
0
The contents are the same and the result is as expected. The breakline was not not taken care of i.e., "\n" needed to be appended, before or after-- old_content.append(str);
内容相同,结果如预期。断条线没有得到处理,即在“old_content.append(str)”之前或之后需要附加“\ n”;
#1
0
I would have avoided trying to re-invent the wheel and use Apache commons - FileUtils.contentEquals()
to compare two files and FileUtils.readFileToString()
to get it as a String
object
我本来避免尝试重新发明*并使用Apache commons - FileUtils.contentEquals()来比较两个文件和FileUtils.readFileToString()来将它作为String对象
Apache commons is an open source widely used library with very premissive license, so using it is usually a good choice.
Apache commons是一个开源广泛使用的库,具有非常优先的许可证,因此使用它通常是一个不错的选择。
P.S. Your bug might be that you are not appending the line-terminator after each line, but it is going to be hard to tell without knowing the contents.
附:您的错误可能是您没有在每行之后附加行终止符,但在不知道内容的情况下很难分辨。
#2
0
The contents are the same and the result is as expected. The breakline was not not taken care of i.e., "\n" needed to be appended, before or after-- old_content.append(str);
内容相同,结果如预期。断条线没有得到处理,即在“old_content.append(str)”之前或之后需要附加“\ n”;