gpt4 book ai didi

java - 即使 file.exists()、file.canRead()、file.canWrite()、file.canExecute() 都返回 true,file.delete() 也会返回 false

转载 作者:IT老高 更新时间:2023-10-28 11:37:27 26 4
gpt4 key购买 nike

我正在尝试使用 FileOutputStream 删除文件,在其中写入内容后。这是我用来编写的代码:

private void writeContent(File file, String fileContent) {
FileOutputStream to;
try {
to = new FileOutputStream(file);
to.write(fileContent.getBytes());
to.flush();
to.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

正如所见,我刷新并关闭了流,但是当我尝试删除时,file.delete() 返回 false。

我在删除前检查了文件是否存在,并且:file.exists(), file.canRead(), file.canWrite() file.canExecute() 都返回 true。在调用这些方法之后,我尝试 file.delete() 并返回 false。

是不是我做错了什么?

最佳答案

Java 中的另一个错误。我很少找到它们,这只是我 10 年职业生涯中的第二个。正如其他人所提到的,这是我的解决方案。我也没用过System.gc()。但在这里,就我而言,这绝对是至关重要的。诡异的?是的!

finally
{
try
{
in.close();
in = null;
out.flush();
out.close();
out = null;
System.gc();
}
catch (IOException e)
{
logger.error(e.getMessage());
e.printStackTrace();
}
}

关于java - 即使 file.exists()、file.canRead()、file.canWrite()、file.canExecute() 都返回 true,file.delete() 也会返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/991489/

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