mercredi 18 novembre 2015

RandomAccessFile Is throwing EOF error why? JAVA

I have a randomaccessfile used to hold student address and names. The strings are fixed sizes of 20 bytes each so I know exactly how much to read in and write out each time.

But I have a function to be able to navigate the .txt file entries using next and previous buttons. The next works fine, however my previous keeps throwing EOF errors. I thought my function was designed not to go out of bounds so I can't figure out why it's throwing the error.

 btPrevious.setOnAction((e) -> {
        try{
            if(raf.getFilePointer() != RECORD_SIZE) //if not at first entry perform action
            readAddress(raf.getFilePointer() - (RECORD_SIZE  * 2)); //go back two records so we are at previous one to display
        }
        catch(IOException ex){
            ex.printStackTrace();
        }
    });

The readAddress simply grabs two strings and displays them in textfields in the main window.

The error is

 java.io.EOFException

What would be a better way to check if we are not at the first entry in the database then read the record behind the current record that was just displayed? I thought RECORD_SIZE * 2 would bring me back 2 records but it seems to not work.




Aucun commentaire:

Enregistrer un commentaire