[英]File won't delete
出於某種原因,我的文件不會使用 f.delete() 刪除; 並且 temp.txt 不會重命名為 Materials.txt。 我不知道出了什么問題,它輸出錯誤,我以管理員身份運行 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);
}
lineno
分配給,因此不能是 int。 也許使用Files.newBufferedReader
。更簡單:
類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);
}
——
我假設正在發生的事情:您沒有文件 temp.txt; 您創建 FileWriter,它將創建 temp.txt 並輸出到它。 但是,您的變量 file1 在 filewriter 創建之前已初始化。 (順便說一句,您可以使用 File#exists 檢查文件是否存在)。 所以你可以做很多事情來改變它,但最簡單的方法是將 new File("temp.txt") 的初始化移動到方法的末尾。 這樣您就可以確定它已創建並且能夠成功重命名它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.