This is how I write out my file.
BufferedReader read = new BufferedReader(new FileReader(filetoreadfrom));
FileOutputStream fileStream = new FileOutputStream(filetowriteto);
DataOutputStream dataStream = new DataOutputStream(fileStream);
String temp;
while((temp = read.readLine()) != null){
String[]arrayTemp = temp.split("\\|");
dataStream.writeInt(Integer.parseInt(arrayTemp[0]));
dataStream.writeInt(Integer.parseInt(arrayTemp[1]));
dataStream.writeUTF(arrayTemp[2]); }
So I am trying to write out a binary file and it seems to be working alright. But when I try to read it back it in, I end up getting IOExceptions.
This is how I read in my file.
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data.bin")));
int one,two,eight;
String three,
while(true){
one = in.readInt();
two = in.readInt();
three = in.readUTF();}
I've been looking at the tutorial page for data streams at
http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html
and from what I understand in the example it shows catches the end of file condition by catching an EOFException? And from what I can see from the api, that is a subclass of IOException, which helps me understand as to why I am getting that.
What I don't understand is how to handle it without having an exception occurring. I have tried doing something like in.read() == -1 then break, but to no avail I still get an exception thrown.