Java DataInputStream 读取操作抛出异常

发布于 2024-10-27 13:04:33 字数 1046 浏览 2 评论 0原文

我试图熟悉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回眸一笑 2024-11-03 13:04:33

而不是

dos.writeBytes("中文字(Chinese)\n");
dos.writeChars("中文字(Chinese)\n");

你需要

dos.writeUTF("中文字(Chinese)\n");

Instead of

dos.writeBytes("中文字(Chinese)\n");
dos.writeChars("中文字(Chinese)\n");

you need

dos.writeUTF("中文字(Chinese)\n");
弃爱 2024-11-03 13:04:33

当您执行 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.

如痴如狂 2024-11-03 13:04:33

您只能使用 readUTF() 读取使用 writeUTF() 写入的项目。

一般来说,对于任何 XXX 的 readXXX()writeXXX() 都是如此(除非您想读取 int 或某些此类的字节,并且您知道自己在做什么)正在做)。

You can only use readUTF() to read items that were written with writeUTF().

This is true in general for readXXX() and writeXXX() for any XXX (unless you want to read the bytes of an int or some such and you know what you're doing).

梦魇绽荼蘼 2024-11-03 13:04:33

我认为此链接会有帮助。抛出异常(来自 Oracle 文档) -

如果此输入流在读取所有字节之前到达末尾。

I think this link will be helpful. The exception is thrown (from oracle docs) -

if this input stream reaches the end before reading all the bytes.

放低过去 2024-11-03 13:04:33

您的文件中的整数后没有 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文