I am trying to write values greater than 256 using DataOupPutStream.write()
method. When i try reading the same value using DataInputStream.read()
it will return 0. So, i used DataOutputStream.writeInt()
and DataInputStream.readInt()
methods to write and retrieve values greater than 256 and it is working fine.
Refer the below code snippet i would like to know the behaviour of the compiler as what it does in the in.readInt()
inside the while
statement.
FileOutputStream fout = new FileOutputStream("T.txt");
BufferedOutputStream buffOut = new BufferedOutputStream(fout);
DataOutputStream out = new DataOutputStream(fout);
Integer output = 0;
out.writeInt(257);
out.writeInt(2);
out.writeInt(2123);
out.writeInt(223);
out.writeInt(2132);
out.close();
FileInputStream fin = new FileInputStream("T.txt");
DataInputStream in = new DataInputStream(fin);
while ((output = in.readInt()) > 0) {
System.out.println(output);
}
The Output when i ran this snippet is :
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at compress.DataIOStream.main(DataIOStream.java:34)
257
2
2123
223
2132
But when i ran in debug mode i get the following output :
2123
223
2132
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at compress.DataIOStream.main(DataIOStream.java:34)