Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

randomaccessfile update java.io.EOFException

Posted on 2006-03-29
21
Medium Priority
?
399 Views
Last Modified: 2008-01-09
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);
            }
      }
0
Comment
Question by:jdzemke
  • 11
  • 6
  • 4
21 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16327302
sounds like your trying to update past the end of the file.
0
 
LVL 92

Expert Comment

by:objects
ID: 16327307
where do u set useFound?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16327426
>>if (file.exists())

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

would be a useful precaution
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jdzemke
ID: 16327534
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
 
LVL 92

Expert Comment

by:objects
ID: 16327581
can u post the stack trace for the exception
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16327662
In your various read methods above, make sure you're not trying to read beyond the end of the file
0
 
LVL 92

Expert Comment

by:objects
ID: 16327687
Are you sure your strings aren't changing in length?
0
 
LVL 92

Expert Comment

by:objects
ID: 16327690
in particular the 'utf' length?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16327701
>>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
 

Author Comment

by:jdzemke
ID: 16327746
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
 
LVL 92

Expert Comment

by:objects
ID: 16327766
Looks like a problem reading strings while searching.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16327779
>>at java.io.RandomAccessFile.readUTF(RandomAccessFile.java:875)

As i said above, you need to check your reads
0
 
LVL 92

Expert Comment

by:objects
ID: 16327792
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
 
LVL 92

Expert Comment

by:objects
ID: 16327800
As soon as your off by one byte your stuffed :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16327815
>>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
 

Author Comment

by:jdzemke
ID: 16328037
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
 
LVL 92

Expert Comment

by:objects
ID: 16328080
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
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 16328121
Adding an index would be a useful addition, ie. to keep track of the file position of each record.
0
 

Author Comment

by:jdzemke
ID: 16329166
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
 
LVL 92

Expert Comment

by:objects
ID: 16329188
great news :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16329944
>>
//dbFile.writeLong(++rowCount);
dbFile.writeLong(rowCount);
>>

Your error was nothing to do with writing to the file
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question