randomaccessfile update java.io.EOFException

I am trying to convert an addrecord method to an updaterecord method when working with a randomaccessfile.  I continue to get a java.io.EOFException error when using the conversion.  Below is the updateRecord method I am using.  Basically, I am seeking based upon a filepointer that I previously retrieved.  The concept is that I search for an existing record, and change it (perform an update per se).  I am not changing any field lengths at this point, just modifying individual characters.   Any insight would be greatly appreciated.

void updateRecord(List data) throws IOException{

            int fieldCount = schema.getFieldCount();

            int [] dataType = schema.getDataType();

            try{
                  File file = new File(dbTableFileName);

                       
                  if (file.exists()){


                        dbFile.seek(usefound);
                        //dbFile.writeLong(++rowCount);
                                //dbFile.writeLong(++rowCount);

                        for (int i = 0; i < fieldCount; i++) {

                              switch (dataType[i]) {
                                           
                                                     case java.sql.Types.VARCHAR:
                                          dbFile.writeUTF((String) data.get(i));

                                    break;

                                    case java.sql.Types.INTEGER:
                                          dbFile.writeInt(((Integer) data.get(i)).intValue());
                                    break;

                                    case java.sql.Types.FLOAT:
                                          dbFile.writeFloat(((Float) data.get(i)).floatValue());
                                    break;

                                    case java.sql.Types.DOUBLE:
                                          dbFile.writeDouble(((Double) data.get(i)).doubleValue());
                                    break;
                              }
                        }

                        //lastFilePointer = (int) dbFile.getFilePointer();
 
                        //dbEOF = lastFilePointer;

                        //updateRowCount(rowCount);

                  }
            }catch(IOException e) {
                  System.out.print("addrecord: " + e);
            }
      }
jdzemkeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
sounds like your trying to update past the end of the file.
0
objectsCommented:
where do u set useFound?
0
CEHJCommented:
>>if (file.exists())

if (file.exists() && usefound >=0 )

would be a useful precaution
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

jdzemkeAuthor Commented:
objects - Thanks for the response.  The usefound is taken from the "return filePointer;" provided below.  This search capability is working and appears to produce the proper filepointer.  Unless, I am not completely understanding how the randomaccessfile works, I am getting the filepointer, doing the seek, and then trying to overwrite any existing data for that row to perform the update??????

private long [] search(int[] column, String[] searchValue) throws IOException{

            long [] filePointer = {0L, 0L};
            long filePoint = 0;

            int colIndex = column[0];

            String junkString;
            int junkInt;
            float junkFloat;
            double junkDouble;

            dbFile.seek(dataStart);

            matchFound = false;

            found:
            do {
                  filePointer[0] = dbFile.getFilePointer();
                  //key
                  dbFile.readLong();
                  //record key

                  for (int i = 0; i < fieldCount; i++) {

                        switch (dataType[i]) {

                              case java.sql.Types.VARCHAR:{
                                    filePoint = dbFile.getFilePointer();

                                    if (i == column[0]){

                                          if (dbFile.readUTF().equalsIgnoreCase(searchValue[0])) {
                                                filePointer[1] = filePoint;
                                                //column

                                                matchFound = true;
                                                break found;
                                          }
                                    }else
                                          junkString = dbFile.readUTF();

                                    break;
                              }

                              case java.sql.Types.INTEGER:{
                                    filePoint = dbFile.getFilePointer();

                                    if (i == column[0]){
                                          if (dbFile.readInt() == Integer.parseInt(searchValue[0])) {
                                                filePointer[1] = filePoint;
                                                //column

                                                matchFound = true;
                                                break found;
                                          }
                                    }else
                                          junkInt = dbFile.readInt();

                                    break;
                              }

                              case java.sql.Types.FLOAT:{
                                    filePoint = dbFile.getFilePointer();

                                    if (i == column[0]){
                                          if (dbFile.readFloat() == Float.parseFloat(searchValue[0])) {
                                                filePointer[1] = filePoint;
                                                //column

                                                matchFound = true;
                                                break found;
                                          }
                                    }else
                                          junkFloat = dbFile.readFloat();

                                    break;
                              }

                              case java.sql.Types.DOUBLE:{
                                    filePoint = dbFile.getFilePointer();

                                    if (i == column[0]){
                                          if (dbFile.readDouble() == Double.parseDouble(searchValue[0])) {
                                                filePointer[1] = filePoint;
                                                //column

                                                matchFound = true;
                                                break found;
                                          }
                                    }else
                                          junkDouble = dbFile.readDouble();

                                    break;
                              }
                        }
                  }

                  lastFilePointer = (int) dbFile.getFilePointer();

                       
                  dbFile.seek(++lastFilePointer);

            } while (dbEOF > lastFilePointer);
           
                 System.out.println("search returned lastFilePointer " + lastFilePointer);

                System.out.println("search returned filePointer " + filePointer);

            return filePointer;
      }
0
objectsCommented:
can u post the stack trace for the exception
0
CEHJCommented:
In your various read methods above, make sure you're not trying to read beyond the end of the file
0
objectsCommented:
Are you sure your strings aren't changing in length?
0
objectsCommented:
in particular the 'utf' length?
0
CEHJCommented:
>>In your various read methods above, make sure you're not trying to read beyond the end of the file

In practice, that means determining before a read that

filePointer + length of data type about to be read <= file length
0
jdzemkeAuthor Commented:
objects - I added the e.printStackTrace() to the updateRecord method.  I then ran Step 1 below with the args listed.  If finished fine.  I then ran Step 2 and got the following output.

catch(IOException e) {
                  //System.out.print("addrecord: " + e);
                       e.printStackTrace();
            }


Step 1
D:\zlib\java2>java DatabaseFind schedule.tbl 2382 64.29 55 "testz" "test2" "test
2" "test2"
Schema for schedule.tbl
There are 5rows of data.
There are 7columns which are:
ID              Integer
Price           Float
Seat            Integer
Origin          String
Destination             String
Destination2            String
Destination3            String

File status             intact

search returned lastFilePointer 262
search returned filePointer [J@130c19b
filepointer in my area 262
usefound in my area 262
this is 2382
RECORD FOUND:   4       2382    64.29   55      test2   test2   test2   test2


this is row.get(1)2382
this is row.get(6)test2
this is row.get(0)4
addRecord fieldCount 7
addRecord dataType 6
addRecord file schedule.tbl
addRecord before dbEOF 359
addRecord after usefound 262
addRecord after dbEOF 359
addRecord before rowCount 5
addRecord after rowCount 5
addRecord in for loop fieldCount 7
addRecord in for loop fieldCount 7
addRecord in for loop fieldCount 7
addRecord in for loop fieldCount 7
addRecord in for loop fieldCount 7
addRecord in for loop fieldCount 7
addRecord in for loop fieldCount 7
addRecord lastFilePointer 262
addRecord dbEOF 359
addRecord rowCount 5

Step 2
D:\zlib\java2>java DatabaseFind schedule.tbl 2382 64.29 55 "testw" "test2" "test
2" "test2"
Schema for schedule.tbl
There are 5rows of data.
There are 7columns which are:
ID              Integer
Price           Float
Seat            Integer
Origin          String
Destination             String
Destination2            String
Destination3            String

File status             intact

Exception in thread "main" java.io.EOFException
        at java.io.RandomAccessFile.readFully(RandomAccessFile.java:373)
        at java.io.DataInputStream.readUTF(DataInputStream.java:565)
        at java.io.RandomAccessFile.readUTF(RandomAccessFile.java:875)
        at DatabaseFind.search(DatabaseFind.java:845)
        at DatabaseFind.useSchedule(DatabaseFind.java:174)
        at DatabaseFind.main(DatabaseFind.java:83)

0
objectsCommented:
Looks like a problem reading strings while searching.
0
CEHJCommented:
>>at java.io.RandomAccessFile.readUTF(RandomAccessFile.java:875)

As i said above, you need to check your reads
0
objectsCommented:
Add some println()'s to print out the value of each field as you read it.
That should give you a better indication when it gets off track.
0
objectsCommented:
As soon as your off by one byte your stuffed :)
0
CEHJCommented:
>>Add some println()'s to print out the value of each field as you read it.

It's not so much the value that's important, but its offset and its length (see above)
0
jdzemkeAuthor Commented:
Is my concept correct (get filepointer, seek, and write) or is there a better way to accomplish so I am not fighting the lengths.  Would it be better to get the filepointer, seek, delete, and then add the record at the end?  I am not familiar with how the delete will work so the filepointers can be reassigned so I keep the same amount of rows.
0
objectsCommented:
no your concept looks fine, you appear to just be getting out of sync somewhere.
Printing out the fields as you read them should help you debug where it is occurring.
0
objectsCommented:
Adding an index would be a useful addition, ie. to keep track of the file position of each record.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdzemkeAuthor Commented:
objects - I uncommented the following code in the updateRecord method and removed the increment for the rowCount and setup some static string lengths for the varchar data.  All of this has worked for now.  I haven't validated it yet, but not repopulating the following variables likely cause changes in the data, etc.  The confirmation of the concept helped to resolve this so I could continue using the existing code.

//dbFile.writeLong(++rowCount);
dbFile.writeLong(rowCount);

lastFilePointer = (int) dbFile.getFilePointer();
 
dbEOF = lastFilePointer;

updateRowCount(rowCount);
0
objectsCommented:
great news :)
0
CEHJCommented:
>>
//dbFile.writeLong(++rowCount);
dbFile.writeLong(rowCount);
>>

Your error was nothing to do with writing to the file
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.