Java DataInputStream 读取操作抛出异常
我试图熟悉 Java IO 类,所以我编写了下面的代码:
public static void main(String[] args)throws IOException {
FileOutputStream fos = new FileOutputStream("fileIO.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
//fos.write(9999);
//bos.write(9999);
dos.writeInt(9999);
dos.writeBytes("中文字(Chinese)\n");
dos.writeChars("中文字(Chinese)\n");
dos.flush();
FileInputStream fis = new FileInputStream("fileIO.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
}
不幸的是,我得到了这个:
9999
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:180)
at java.io.DataInputStream.readUTF(DataInputStream.java:592)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at IO.main(IO.java:34)
谁能指出原因吗?谢谢。
I'm trying to familiarize myself with Java IO classes, so I wrote the code below:
public static void main(String[] args)throws IOException {
FileOutputStream fos = new FileOutputStream("fileIO.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
//fos.write(9999);
//bos.write(9999);
dos.writeInt(9999);
dos.writeBytes("中文字(Chinese)\n");
dos.writeChars("中文字(Chinese)\n");
dos.flush();
FileInputStream fis = new FileInputStream("fileIO.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
}
Unfortunately, I get this:
9999
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:180)
at java.io.DataInputStream.readUTF(DataInputStream.java:592)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at IO.main(IO.java:34)
Could anyone point out why? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
而不是
你需要
Instead of
you need
当您执行 readUTF 时,前两个字节用于长度。这意味着如果你有随机字节(不是来自 writeUTF),你将尝试读取很长的字符串,并且由于没有足够的数据,你将得到 EOFException。
When you perform a readUTF, the first two byte are used for the length. This means if you have random bytes there (not from writeUTF) you will attempt to read very long string instead and as there is not enough data, you will get EOFException.
您只能使用
readUTF()
读取使用writeUTF()
写入的项目。一般来说,对于任何 XXX 的
readXXX()
和writeXXX()
都是如此(除非您想读取 int 或某些此类的字节,并且您知道自己在做什么)正在做)。You can only use
readUTF()
to read items that were written withwriteUTF()
.This is true in general for
readXXX()
andwriteXXX()
for any XXX (unless you want to read the bytes of an int or some such and you know what you're doing).我认为此链接会有帮助。抛出异常(来自 Oracle 文档) -
I think this link will be helpful. The exception is thrown (from oracle docs) -
您的文件中的整数后没有 UTF 字符。当您尝试读取 UTF 时,会出现文件结尾,因此您会遇到例外情况。
在手动读取和检查文件之前尝试在调试模式下停止,然后你有什么?
You haven't UTF char after Integer number in your file. When you're trying read UTF there is End of File so you have the exception.
Try in debug mode stop before you read and check manually to your file, what do you have then?