Jud*_*tor 9 java exception ioexception fileinputstream
我对程序最近开始抛出的错误感到困惑.
java.io.IOException: No space left on device
at java.io.FileInputStream.close0(Native Method)
at java.io.FileInputStream.close(FileInputStream.java:259)
at java.io.FilterInputStream.close(FilterInputStream.java:155)
Run Code Online (Sandbox Code Playgroud)
我假设因为这是一个FileInputStream,该文件被保存在内存中,而不是物理磁盘上.内存级别看起来很棒,磁盘空间也是如此.这特别令人困惑,因为它发生在FileInputStream的结束时.感谢您对如何发生这种情况的任何解释.
编辑:审查代码
if (this.file.exists()) {
DataInputStream is = new DataInputStream(new FileInputStream(this.file));
this.startDate = new DateTime(is.readLong(), this.timeZone);
this.endDate = new DateTime(is.readLong(), this.timeZone);
is.close();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我只打开文件,阅读一些内容,然后关闭文件.
在这种情况下,将从关闭流的方法IOException
中抛出。native
它被定义为抛出异常的原因是因为该close
操作执行了final flush
- 因此,如果IOException
在刷新期间发生异常,它将被抛出。
您收到的异常有多种原因:
您可能缺乏对特定文件夹的写入权限。
您可能已经超出了配额。
我个人也建议大家使用下面的方法来关闭流:
if (this.file.exists()) {
try {
DataInputStream is = new DataInputStream(new FileInputStream(this.file));
this.startDate = new DateTime(is.readLong(), this.timeZone);
this.endDate = new DateTime(is.readLong(), this.timeZone);
} catch (Exception ex) {
// Handle the exception here
} finally {
is.close();
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用不会引发异常的IOUtils方法,因为在您的情况下,您不会更改文件,并且您可能对该方法的结果不感兴趣。closeQuietly
close
编辑:
亨利是对的。我读完InputStream
后在心里自动将其改为OutputStream
。
close
对 的操作不会InputStream
更改文件本身,但可以更改metadata
文件的 - 例如上次访问时间等。