gpt4 book ai didi

java - 更新数据列表时出现问题

转载 作者:行者123 更新时间:2023-12-02 08:14:49 25 4
gpt4 key购买 nike

private static void deleteProxy(File proxyOld, String host, int port) {
try {
String lines, tempAdd;
boolean removeLine = false;
File proxyNew = new File("proxies_" + "cleaner$tmp");
BufferedReader fileStream = new BufferedReader(new InputStreamReader(new FileInputStream(proxyOld)));
BufferedWriter replace = new BufferedWriter(new FileWriter(proxyNew));
while ((lines = fileStream.readLine()) != null) {
tempAdd = lines.trim();
if (lines.trim().equals(host + ":" + port)) {
removeLine = true;
}
if (!removeLine) {
replace.write(tempAdd);
replace.newLine();
}
}
fileStream.close();
replace.close();
proxyOld.delete();
proxyNew.renameTo(proxyOld);
} catch (Exception e) {
e.printStackTrace();
}
}

调用函数:

File x = new File("proxies.txt");//is calling a new file the reason why it's being flushed out?
deleteProxy(x, host, port);

在运行程序之前,文件 proxies.txt 中包含数据。然而,当我运行该程序时,它似乎被冲掉了。它变成空的。

我注意到程序运行时,如果我将鼠标移到文件 proxies.txt 上,Windows 会显示“修改日期” 并且显示的时间为当前时间,或上次执行函数 deleteProxy(...) 的时间。

有人知道我做错了什么吗?为什么列表不会更新而不是显示为空?

更新的代码:

private static void deleteProxy(File proxyOld, String host, int port) {
try {
String lines, tempAdd;
boolean removeLine = false;
File proxyNew = new File("proxies_" + "cleaner$tmp");
FileInputStream in = new FileInputStream(proxyOld);
InputStreamReader read = new InputStreamReader(in);
BufferedReader fileStream = new BufferedReader(read);
FileWriter write = new FileWriter(proxyNew);
BufferedWriter replace = new BufferedWriter(write);

while ((lines = fileStream.readLine()) != null) {
tempAdd = lines.trim();
if (lines.trim().equals(host + ":" + port)) {
removeLine = true;
}
if (!removeLine) {
replace.write(tempAdd);
replace.newLine();
}
}
in.close();
read.close();
fileStream.close();
write.close();
replace.close();

if (proxyOld.delete()) {
throw new Exception("Error deleting " + proxyOld);
}

if (proxyNew.renameTo(proxyOld)) {
throw new Exception("Error renaming " + proxyOld);
}
} catch (Exception e) {
e.printStackTrace();
}
}

运行更新后的代码可以很好地删除 proxies.txt 但无法创建新文件:\也许我应该找到一种新的方法来更新文本文件,你有什么建议吗?

最佳答案

根据File.renameTo() documentation,您的重命名操作可能不起作用。 :

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

所以基本上,您正在删除旧文件,并且不能保证新文件会取代它。您必须检查File.renameTo()的返回值:

if(proxyNew.renameTo(proxyOld)){
throw new Exception("Could not rename proxyNew to proxyOld");
}

至于您的 renameTo 可能失败的原因:您没有关闭打开以从旧文件中读取的嵌套流集,因此操作系统可能仍会考虑使用抽象路径名存在。尝试确保关闭所有打开的嵌套流。

关于java - 更新数据列表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6659216/

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