File file = new File(EMP_DATA_FILE);
File file2 = new File("D:/eclipse-jee-mars-R-win32-x86_64/项目/HRMIS/records-new.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
PrintWriter writer = null;
try {
writer = new PrintWriter(file2);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String line;
try {
while ((line = reader.readLine()) != null) {
// 判断条件,根据自己的情况书写,会删除所有符合条件的行
if (line.startsWith(payroll)) {
// 读取后面的几行,废弃
continue;
}
writer.println(line);
writer.flush();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.close();
// 删除老文件
file.delete();
file2.renameTo(file);
System.out.print("\nRecord deleted.\n");
String cmd = "del "+ EMP_DATA_FILE ;
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("cmd /c "+cmd); //cmd为String,其内容为对应系统的删除命令,例如:在win下,应该写成:String cmd = "del "+你的文件路径以及文件名;
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
为什么把这段代码放在了这方法的开头就可以了,但该段程序并未删除文档,好像是把文档的各种流关闭了,然后再执行delete方法就可以删除了,求大神们解答下为什么啊????????,File的delete删除。到底要关闭那些流啊?????