我有以下代码,但它似乎没有工作..有人知道为什么吗?基本上我正在创建一个包含所有行的新文件,除了那些包含我要删除的两个字符串但代码似乎不起作用的行。
public void removelines(String name, String email){
try{
//Create objects for our file and the temp file.
File f = new File("addy");
File temp = new File(f.getAbsolutePath()+ "temp");
//Our readers.
BufferedReader buf = new BufferedReader(new FileReader("addy"));
PrintWriter print = new PrintWriter(new FileWriter(temp));
String line = null;
while ((line = buf.readLine()) != null) {
//put all data not equal to that to be removed into the new file.
if(!line.trim().equals(name) || !line.trim().equals(email)){
print.println(line);
print.flush();
}
}
print.close();
buf.close();
f.delete();
temp.renameTo(f);
} catch (Exception e){
JOptionPane.showMessageDialog(null, "ERROR:" + e );
}
}