Java プログラミングでファイルを削除してから名前を変更する際に助けが必要です。私の問題は、元のファイルを削除できず、2 番目のファイルの名前を変更できないことです。これがスニペットコードです。任意の提案をいただければ幸いです。
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public void deleteLine(String content) {
try {
File inFile = new File("football.dat");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
File tempFile = new File(inFile.getAbsolutePath() + "2");
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile), true);
String linetobeempty = null;
while ((linetobeempty = br.readLine()) != null) {
if (!linetobeempty.trim().equals(content)) {
pw.println(linetobeempty);
pw.flush(); System.out.println(linetobeempty);
}
}
pw.close();
br.close();
boolean b = inFile.delete();
if (!b) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile)) {
System.out.println("Could not rename file");
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}