出于某种原因,我的文件不会使用 f.delete() 删除;并且 temp.txt 不会重命名为 Materials.txt。我不知道出了什么问题,它输出了 false,我以管理员身份运行 NetBeans,以确保它有权删除该文件,并且在编辑一行之前的代码工作正常,但事实上它是临时的,没有被更改为Materials.txt。感谢任何帮助,谢谢!
try {
DefaultTableModel model= (DefaultTableModel)Table.getModel();
int selectedRowIndex = Table.getSelectedRow();
File f= new File("Materials.txt");
File file1= new File("temp.txt");
FileReader fr= new FileReader("Materials.txt");
BufferedReader br= new BufferedReader(fr);
FileWriter fw= new FileWriter("temp.txt", true);
String updated = (jTextField1.getText()+","+jTextField2.getText()+","+jTextField3.getText()+","+jTextField4.getText()+","+jTextField5.getText()+","+jTextField6.getText()+"\r\n");
String temp;
int a=0;
while (a<=selectedRowIndex)
{
a++;
String line= br.readLine();
fw.write(line+"\r\n");
}
br.readLine();
fw.write(updated);
while (br.ready()==true)
{
temp=br.readLine();
fw.write(temp+"\r\n");
}
fw.close();
br.close();
fr.close();
System.out.println(f.delete());
file1.renameTo(f);
}
catch (IOException e){
System.err.println(e);
}
编辑:更新的代码试图实现建议的解决方案,“void updateMaterialsFile(int UpdatedLineno = 0, String Updated) throws IOException {”行抛出错误,指出表达式的非法开始,期望; (多次)而不是声明。一如既往。
try {
DefaultTableModel model= (DefaultTableModel)Table.getModel();
int selectedRowIndex = Table.getSelectedRow();
String updated = (jTextField1.getText()+","+jTextField2.getText()+","+jTextField3.getText()+","+jTextField4.getText()+","+jTextField5.getText()+","+jTextField6.getText()+"\r\n");
void updateMaterialsFile(int updatedLineno = 0, String updated) throws IOException {
Path materialsPath = Paths.get("Materials.txt");
Path tempPath = materialsPath.resolveSibling("temp.txt");
try (BufferedReader fr = Files.newBufferedReader(materialsPath);
BufferedWriter fw = Files.newBufferedWriter(tempPath);) {
for (int lineno = 0; ; ++lineno) {
String line = fr.readLine();
if (line == null) {
break;
}
fw.write(lineno == updatedLineno ? updated : line);
fw.write("\r\n");
}
} // Automatically closes fr and fw
Files.move(tempPath, materialsPath, StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e){
System.err.println(e);
}
最佳答案
不完全清楚删除的时间问题,因为所有内容似乎至少被关闭一次。
void updateMaterialsFile(int updatedLineno, String updated) throws IOException {
Path materialsPath = Paths.get("Materials.txt");
Path tempPath = materialsPath.resolveSibling("temp.txt");
try (Stream<String> lines = Files.lines(materialsPath);
BufferedWriter fw = Files.newBufferedWriter(tempPath)) {
AtomicInteger lineno = new AtomicInteger();
lines.forEach(line -> {
int lno = lineno.getAndIncrement();
try {
fw.write(lno == updatedLineno ? updated : line);
fw.write("\r\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (RuntimeException e) {
throw new IOException(e.getCause());
}
Files.move(tempPath, materialsPath, StandardCopyOption.REPLACE_EXISTING);
}
- 上面的代码使用了较新的样式,临时文件在 Path 中使用相同的文件系统。
- 重命名(移动)一步完成删除。
- 您还可以使用临时文件(请参阅文件),该文件可能位于更快的文件系统上。
- 使用 try-with 资源会自动关闭,即使在返回、中断、引发异常时也是如此。
- Stream 版本用于读取,它有一个缺点:传递的 lambda 可能不会抛出 IOException。另外,“循环计数器”
lineno
不能被赋值,因此不能是 int。也许使用Files.newBufferedReader
。
更简单:
类(class)Files人们应该知道,因为它提供了许多实用程序调用。
/** @param updateLineno counted from 0. */
void updateMaterialsFile(int updatedLineno, String updated) throws IOException {
Path materialsPath = Paths.get("Materials.txt");
Path tempPath = materialsPath.resolveSibling("temp.txt");
try (BufferedReader fr = Files.newBufferedReader(materialsPath);
BufferedWriter fw = Files.newBufferedWriter(tempPath)) {
for (int lineno = 0; ; ++lineno) {
String line = fr.readLine();
if (line == null) {
break;
}
fw.write(lineno == updatedLineno ? updated : line);
fw.write("\r\n");
}
} // Automatically closes fr and fw
Files.move(tempPath, materialsPath, StandardCopyOption.REPLACE_EXISTING);
}
-
关于java - 文件不会删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59858936/