public void removeLine(String s) throws IOException, FileNotFoundException{
File tempFile = new File("temp.txt");
FileInputStream reader = new FileInputStream(sharkFile);
Scanner scanner = new Scanner(reader);
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile, true));
String currentLine;
while(scanner.hasNextLine()){
currentLine = scanner.nextLine();
String trimmedLine = currentLine.trim();
System.out.println(trimmedLine);
trimmedLine.equals(sharkName);
if(trimmedLine.equals(sharkName)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
scanner.close();
scanner = null;
reader.close();
writer.flush();
writer.close();
writer = null;
System.gc();
if(!sharkFile.delete()){
System.out.println("Could not delete file d");
return;
}
if(!tempFile.renameTo(sharkFile)){
System.out.println("Could not rename file");
return;
}
}
私はstackoverflowで数多くのスレッドを実行し、それらの変更を実装しましたが、ファイルは削除されません。ヘルプをよろしくお願いいたします。なぜ私のファイルは削除されませんか?
ユーザー権限を確認しましたか?あなたはどんなエラーを出していますか? – johan855
IO例外があればどこかにキャッチしていますか? –
'Files.delete()'を使う;少なくとも失敗時には意味のある例外が発生します。また、 'System.gc()'がなぜですか? – fge