In an attempt to understand the various I/O streams that Java supports, and their relationships to each other, I came across this bit of code, which on the surface, looks like it should work.
However, on the output side, when firtst reading from BufferedReader input and then from DataInputStream input, the program throws an IO exception. he exception is not thrown when changing the read from DataInputStream to BufferedReader; only the other way around.
Does anyone have any suggestions as to why this is so? Is it a software bug or does the program not use the streams properly?
Thanks,
Vic
However, on the output side, when firtst reading from BufferedReader input and then from DataInputStream input, the program throws an IO exception. he exception is not thrown when changing the read from DataInputStream to BufferedReader; only the other way around.
Does anyone have any suggestions as to why this is so? Is it a software bug or does the program not use the streams properly?
Code:
import java.io.*;
public class IOProblem {
public static void main(String[] args) throws IOException
{
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
out.writeDouble(3.14159);
out.writeBytes("That was the value of pi\n");
out.writeBytes("This is pi/2:\n");
out.writeDouble(3.14159/2);
out.close();
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
BufferedReader inbr = new BufferedReader(new InputStreamReader(in));
System.out.println(in.readDouble());
System.out.println(inbr.readLine());
System.out.println(inbr.readLine());
System.out.println(in.readDouble());
}
}
Thanks,
Vic