我试图使用DataOupPutStream.write()方法写入大于256的值.当我尝试使用DataInputStream.read()读取相同的值时,它将返回0.因此,我使用DataOutputStream.writeInt()和DataInputStream.readInt()方法来编写和检索大于256的值,并且它工作正常.
请参阅下面的代码片段我想知道编译器的行为,就像它在while语句中的in.readInt()中所做的那样.
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);
}
运行此代码段时的输出是:
Exception in thread "main" java.io.