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

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

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

发布评论

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

>www.elefans.com

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