Reading integer values from binary file using Java

戏子无情 提交于 2019-12-04 13:10:23

The readInt() method is a method like any other. You are getting an EOFException because that's what the Javadoc for readInt() says will happen when you reach the end of the file.


When I run

DataOutputStream out = new DataOutputStream(new FileOutputStream("T.txt"));
out.writeInt(257);
out.writeInt(2);
out.writeInt(2123);
out.writeInt(223);
out.writeInt(2132);
out.close();

DataInputStream in = new DataInputStream(new FileInputStream("T.txt"));
try {
    while (true) 
        System.out.println(in.readInt());
} catch (EOFException ignored) {
    System.out.println("[EOF]");
}
in.close();

I get this in normal and debug mode.

257
2
2123
223
2132
[EOF]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!