我有下面的代码,但它似乎没有工作..任何人有任何想法为什么?基本上我创建了一个包含所有行的新文件,除了那些包含我想要移除的两个字符串的行,但代码似乎没有工作。删除文件中的行
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);
}
}
什么问题? – pgras 2010-11-29 12:48:13