gpt4 book ai didi

java - Linux 与 Windows File.delete()

转载 作者:行者123 更新时间:2023-11-29 03:33:54 25 4
gpt4 key购买 nike

与 Linux 相比,我不太了解 Windows 如何处理文件。我正在尝试删除我用作“数据库”的文件中的条目(它只是一个常规文件)。不幸的是,Java 没有提供这样做的方法,所以我必须将除要删除的条目之外的所有条目复制到一个临时文件中,删除旧数据库,创建一个新的空数据库文件,将临时文件的内容复制到新的数据库文件,并通过删除临时文件来完成。

这是我的函数的代码:

private void removeSelectedItem(String entryToRemove) {

//original database file
File database = new File("catalog");

//all entries except selected will be written here first
File temp = new File("temp");

boolean endOfFileFlag = false;
String line = "";

try {

//used for reading original database file
BufferedReader databaseReader =
new BufferedReader(new FileReader(database));

//used for writing to newly-created temp file
BufferedWriter tempWriter =
new BufferedWriter(new FileWriter(temp, true));

/*
* Read original database line by line and test to see if the line
* has the course prefix and id. If it does, it won't be written to
* the temp file.
*/
while (endOfFileFlag == false) {

line = databaseReader.readLine();

/*
* This code is ugly. If possible, this check needs to be
* made in the conditions of the while loop.
*/
if (line == null) {

endOfFileFlag = true;
break;

}

//tests to see if the line is to be removed
if ( !line.contains(entryToRemove))
tempWriter.write(line + "\r\n");

}

endOfFileFlag = false; //reset this for the next loop
databaseReader.close(); //database will be deleted
tempWriter.close(); //temp file is written
database.delete(); //delete this to create a new updated one below

database.createNewFile();

//writes to the new database
BufferedWriter databaseWriter =
new BufferedWriter(new FileWriter(database, true));

//reads from the temp file
BufferedReader tempReader =
new BufferedReader(new FileReader(temp));

//read temp line by line and add each line to a new catalog file
while (endOfFileFlag == false) {

line = tempReader.readLine();

/*
* This code is ugly. If possible, this check needs to be made
* in the conditions of the while loop. Attempts thus far have
*/
if(line == null){

endOfFileFlag = true;
break;

}

databaseWriter.write(line + "\r\n");

}

tempReader.close(); //temp will be deleted
databaseWriter.close(); //new database has been written
temp.delete(); //temp file no longer needed

setUpInfo(); //update the lists with the new catalog info

}catch(IOException e){

System.out.println("removeSelectedItem()");
e.printStackTrace();

}

}

此代码(“\r\n”除外,它在 Linux 中只是“\n”)在 Linux 下完美执行,但在 Windows 中我发现当我激活事件处理程序以删除条目时, 该程序只会添加额外的条目。经过调试我发现调用 database.delete() 实际上并没有删除数据库文件,而调用 temp.delete() 是删除临时文件(就像它应该的那样)。我发现这很奇怪,所以我检查了文件的权限,它们被设置为“读/写”。我尝试了以下在互联网上找到的修复方法:

endOfFileFlag = false; //reset this for the next loop
databaseReader.close(); //database will be deleted
tempWriter.close(); //temp file is written
database.delete(); //delete this to create a new updated one below
//new code
database = null;
System.gc();

但是没有用。我想不出还有什么可能发生。

最佳答案

据我所知,没有操作系统支持删除文件的一部分(结尾除外)

您无法删除已打开的文件,因此您必须确保已在所有位置关闭它,但您可以创建一个临时文件并将其重命名为原始文件。 (不需要复制回来)

我可以这样写

public static void removeLine(String filename, String line) {
File from = new File(filename);
File tmp = new File(filename + ".tmp");
PrintWriter pw = null;
BufferedReader br = null;
try {
pw = new PrintWriter(tmp);
br = new BufferedReader(new FileReader(from));
boolean found = false;
for (String line2; (line2 = br.readLine()) != null; )
if (line2.equals(line))
found = true;
else
pw.println(line2);
pw.close();
br.close();
if (found) {
from.delete();
tmp.renameTo(from);
} else {
tmp.delete();
}
} catch (IOException e) {
// log error.
try { if (br != null) br.close(); } catch (IOException ignored) { }
if (pw != null) pw.close();
}
}

关于java - Linux 与 Windows File.delete(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16527269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com