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

编程入门 行业动态 更新时间:2024-10-27 12:37:12
本文介绍了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.

问题是我没有解释.

我不知道 BufferedReader 和 FileOutputStream 不兼容.

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

更多推荐

file.delete() 返回 false,即使 file.exists()、file.canRead()、file.canWrite()、file.canE

本文发布于:2023-06-08 14:30:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/583104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:false   exists   file   delete   canExecute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!