[英]Deleting and renaming file
所以我試圖從文件中刪除一行數據,我通過打開一個新文件並寫下與我想刪除的數據不匹配的所有信息來成功完成。 問題是,在我完成之后,我想刪除我的原始文件,然后將新文件重命名為不包括我要刪除的信息,與原始文件同名。 我在代碼中添加了這樣做,但由於某種原因它不起作用。
public static void delete() throws IOException
{
File inputFile = new File("Elements.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String element = JOptionPane.showInputDialog(null, "Enter the name of the Element you wish to delete.", "Remove an Element.", JOptionPane.INFORMATION_MESSAGE);;
String currentLine;
while((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
if(trimmedLine.startsWith(element)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
JOptionPane.showMessageDialog(null, "Data has been removed from the file: Elements.txt");
}
正如你可以看到底部附近,我有這些線:
inputFile.delete();
tempFile.renameTo(inputFile);
這些行用於刪除我的原始文件(inputFile),然后將我的新文件(tempFile)重命名為原始文件所具有的文件名。 然而,在運行代碼之后,我只得到一個名為“myTempFile.txt”的文件,該文件已成功刪除了我想要的數據行,但我的原始文件仍然存在且未刪除,新文件也未重命名為原始文件。
知道為什么會這樣嗎?
使用java.nio.file
API 。 這是2015年。
final Path src = Paths.get("Elements.txt").toAbsolutePath();
final Path tmp = src.resolveSibling("Elements.txt.new");
try (
final BufferedReader reader = Files.newBufferedReader(src, StandardCharsets.UTF_8);
final BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8,
StandardOpenOption.CREATE_NEW);
) {
// yadda yadda
}
Files.move(tmp, src, StandardCopyOption.REPLACE_EXISTING);
File
不可靠 。 它一直都是。
在這種情況下,我會開始擺弄,閱讀文檔,也許谷歌搜索一下。 但我也會給你一個答案!
inputFile.delete();
這可能會出錯,例如,如果您在文本編輯器中打開了文件。 幸運的是delete()
返回一個布爾值,試試看!
此外,正如Niels正確提到的,如果您可以訪問Java 7,則File.renameTo()
非常難以使用files.nio
替代方案。 在Java 7中,您可以使用Files.move(Path source, Path target, CopyOption... options)
Java 7文件的文檔: http : //docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
但是你的代碼對我來說正常。 我只更改文件的路徑,並確保文件未在編輯器中打開
public class NewClass {
public static void main(String[] args) {
try {
delete();
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void delete() throws IOException {
File inputFile = new File("C:\\Users\\olyjosh\\Desktop\\Elements.txt");
File tempFile = new File("C:\\Users\\olyjosh\\Desktop\\myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String element = JOptionPane.showInputDialog(null, "Enter the name of the Element you wish to delete.", "Remove an Element.", JOptionPane.INFORMATION_MESSAGE);;
String currentLine;
while ((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
if (trimmedLine.startsWith(element)) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
JOptionPane.showMessageDialog(null, "Data has been removed from the file: Elements.txt");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.