file.delete()返回false,即使file.exists(),file.canRead(),file.canWrite(),file.canExecute()都返回true [英] file.delete() returns false even though file.exists(), file.canRead(), file.canWrite(), file.canExecute() all return true

查看:1034
本文介绍了file.delete()返回false,即使file.exists(),file.canRead(),file.canWrite(),file.canExecute()都返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FileOutputStream 删除一个文件。这是我用于写作的代码:

I'm trying to delete a file, after writing something in it, with FileOutputStream. This is the code I use for writing:

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。

As it is seen, I flush and close the stream, but when I try to delete, file.delete() returns false.

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

I checked before deletion to see if the file exists, and: file.exists(), file.canRead(), file.canWrite(), file.canExecute() all return true. Just after calling these methods I try file.delete() and returns false.

有什么我有做错了?

推荐答案

这个技巧很奇怪。事情是当我以前读过该文件的内容时,我使用了 BufferedReader 。阅读后,我关闭了缓冲区。

It was pretty odd the trick that worked. The thing is when I have previously read the content of the file, I used BufferedReader. After reading, I closed the buffer.

同时我切换了,现在我正在使用 FileInputStream 阅读内容。完成阅读后,我关闭流。现在它正在工作。

Meanwhile I switched and now I'm reading the content using FileInputStream. Also after finishing reading I close the stream. And now it's working.

问题是我没有解释。

我不知道 BufferedReader FileOutputStream 不兼容。

这篇关于file.delete()返回false,即使file.exists(),file.canRead(),file.canWrite(),file.canExecute()都返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆