0

I have taken in a text file and converted it into a binary by writing the objects to a binary file, then i am attempting to read from the binary file, which works all the way through till the last record and then it throws the IO exception and never gets to my last print statement.

Below is my code that is triggering the error

    try
    {
        ObjectInputStream objinputStream2 = new ObjectInputStream(new FileInputStream("binaryFile"));
        for (int run = 0; run<count;count++)
        {
            Record readone = (Record) objinputStream2.readObject();
            System.out.print(readone); 
            System.out.println("");

        }
        System.out.println("Reading completed for all" + count + " records. ");
       // objinputStream2.close();
    }

    catch(FileNotFoundException e){ // Catches object

        System.out.println("Sorry File not found"); // Error Message       
    }

    catch(ClassNotFoundException e){ // Catches object
        System.out.println("Sorry class not found"); // Error Message
    }
    catch(IOException e){ // Catches object
        System.out.println("Problem with file output."); // Error Message
        e.printStackTrace();
    }

any help is appreciated

Stack trace:

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:730)
5
  • 1
    edit the question to provide us the stacktrace Commented Nov 4, 2014 at 16:56
  • 2
    How is count being populated? You may have fewer records in the file than you think. Commented Nov 4, 2014 at 16:58
  • Include the full exception. Right now, you're only providing the stack trace, not the full exception. Commented Nov 4, 2014 at 17:00
  • count is being populated by a while statement: while(inputStream.hasNext())
    – Gio
    Commented Nov 4, 2014 at 17:01
  • That was it, thank you guys! I appreciate all the help!
    – Gio
    Commented Nov 4, 2014 at 17:16

2 Answers 2

0

You have to rethink the loop condition: for (int run = 0; run<count;count++).

Neither run nor count are modified inside the loop, so it will run forever (until you try to read a record from your file and an Exception occurs).

I think it should be something like:

int count = 0;
for (int run = 0; run<numberOfRecordsInFile; run++)
{
    Record readone = (Record) objinputStream2.readObject();
    System.out.print(readone); 
    System.out.println("");
    count++;
}
System.out.println("Reading completed for all" + count + " records. ");
0

Your code:

for (int run = 0; run<count;count++)

needs to be:

for (int run = 0; run<count;run++)

Each time it runs, it tries to grab more lines than it can, because run will always be less than count the way you have it written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.