import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestEOF {
// Throw exceptions to console:
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new BufferedInputStream(
new FileInputStream("TestEOF.txt")));
while (in.available() != 0)
System.out.print((char)(in.readByte()));
//testDataOutputStream
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream("ABC.txt")));
out.writeDouble(12.33333333);
out.writeDouble(82918293.3334893320109);
out.flush();
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream("ABC.txt")));
while (dis.available() != 0)
System.out.print(Byte.toString(dis.readByte()));
System.out.println(dis.readByte());
}
} // /:~
Hi, guys. I thought char needs two bytes in java, so "System.out.print((char)(in.readByte()))" cannot work or it should give me wrong answer since every time it converts one byte to a char. However, it works well: Whatever I put in the "TestEOF.txt" file can be printed out correctly.
For the part starting from "//testDataOutputStream", Byte.toString(dis.readByte()) works well, but dis.readByte() throws an exception. It really confuses me.
Anyone can help? Thank you very much.