0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2677821
  • 106
  • 9
  • What does the IOException say? Give us the stack trace. – Bernhard Barker Sep 18 '13 at 09:41
  • The thread you posted says it clearly __Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values__ – Shashank Kadne Sep 18 '13 at 09:49
  • @ShashankKadne That statement is incorrect. readLine() returns null at end of stream. It does not throw EOFException. – user207421 Sep 18 '13 at 09:53
  • Wasn't that method deprecated ? http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readLine%28%29 – Shashank Kadne Sep 18 '13 at 09:56
  • @ShashankKadne Certainly. So? – user207421 Sep 18 '13 at 09:57
  • Well, I am actually doing a try catch around it so that it catches an IOException e and it just prints out a line whenever it catches it so I know that something was caught. – user2677821 Sep 18 '13 at 10:11
  • @user2677821 For testing purposes, you can do `e.printStackTrace()` instead. Or `System.out.println(e.getMessage())`. But you should be handling an `EOFException` separately as EJP suggested. – Bernhard Barker Sep 18 '13 at 11:30

1 Answers1

0

The API is already designed. You can't change how it works. Catch the EOFException separately from IOException, close the stream, and break. When you catch IOException, log the error, close the stream, and break.

user207421
  • 305,947
  • 44
  • 307
  • 483