我正在使用 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
发布评论