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

查看:53
本文介绍了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() 都返回真.在调用这些方法之后,我尝试了 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.

问题是我没有解释.

我不知道 BufferedReaderFileOutputStream 不兼容.

I don't know BufferedReader and FileOutputStream to be incompatible.

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

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