Results 1 to 6 of 6
  1. #1
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default An Unexpected EOFException

    Hi, I'm trying to write data for three products to a random access file, but I have a problem. Each product has a code and a price. After writing them to my file, I want to get to the second field or product. I do this by getting the length for each record, and multiplying it by the field number I want to go to. I use the result for the seek() method of the RandomAccessFile object. It is giving me an EOFException, but when I use 0 for the seek method i get the data for the first poduct or field. Could someone help me?

    Java Code:
     public static void main(String[] args) {   
            final int RECORD_LENGTH = 16;
    
            try(RandomAccessFile productsOutput = new RandomAccessFile("C:\\Users\\alfonso\\Desktop\\BinaryTestRandom.ran", "rw")) {
                String[] codes = {"java", "jsps", "txtp"};
                double[] prices = {49.5, 19.9, 29.9};
                for(int i = 0; i < codes.length; i++) {
                    productsOutput.writeUTF(codes[i]);
                    productsOutput.writeDouble(prices[i]);
                }
            }
            catch(IOException e) {
                e.printStackTrace();
            }
            
            try(RandomAccessFile productsInput = new RandomAccessFile("C:\\Users\\alfonso\\Desktop\\BinaryTestRandom.ran", "r")) {
                int recordNumber = 3;
                productsInput.seek((recordNumber - 1) * RECORD_LENGTH);
                System.out.print(productsInput.readUTF() + " ");
                System.out.println(productsInput.readDouble());          
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: An Unexpected EOFException

    The problem is that your record length is wrong. It should be 14 not 16. From the Javadoc for writeUTF:

    "Writes a string to the file using modified UTF-8 encoding in a machine-independent manner. First,
    two bytes are written to the file, starting at the current file pointer, as if by the writeShort
    method giving the number of bytes to follow. This value is the number of bytes actually written out,
    not the length of the string. Following the length, each character of the string is output, in sequence,
    using the modified UTF-8 encoding for each character."

    So your strings are 2 + 4 = 6.

    A double is written as a long which adds 8 bytes to get 14.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: An Unexpected EOFException

    Quote Originally Posted by jim829 View Post
    The problem is that your record length is wrong. It should be 14 not 16. From the Javadoc for writeUTF:

    "Writes a string to the file using modified UTF-8 encoding in a machine-independent manner. First,
    two bytes are written to the file, starting at the current file pointer, as if by the writeShort
    method giving the number of bytes to follow. This value is the number of bytes actually written out,
    not the length of the string. Following the length, each character of the string is output, in sequence,
    using the modified UTF-8 encoding for each character."

    So your strings are 2 + 4 = 6.

    A double is written as a long which adds 8 bytes to get 14.

    Regards,
    Jim
    Thanks, buddy, it's all fixed.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: An Unexpected EOFException

    However, although what I said worked, you need to be careful. The length of the string is not necessarily equal to the number of bytes written. So in my opinion, it would be difficult to accurately calculate the location of the next record since the modified UTF encoding could be 1, 2, or 3 bytes. The DataOutput interface description gives a better idea of how the encoding works than the implementation description. As long as you use 7 bit ASCII you should be okay (as per the following extract):

    If a character c is in the range \u0001 through \u007f, it is represented by one byte:

    Otherwise, it could be more than one byte.

    Regards,
    Jim
    Last edited by jim829; 07-12-2014 at 02:57 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: An Unexpected EOFException

    Quote Originally Posted by jim829 View Post
    However, although what I said worked, you need to be careful. The length of the string is not necessarily equal to the number of bytes written. So in my opinion, it would be difficult to accurately calculate the location of the next record since the modified UTF encoding could be 1, 2, or 3 bytes. The DataOutput interface description gives a better idea of how the encoding works than the implementation description. As long as you use 7 bit ASCII you should be okay (as per the following extract):

    If a character c is in the range \u0001 through \u007f, it is represented by one byte:

    Otherwise, it could be more than one byte.

    Regards,
    Jim
    Yes, I am aware of this. Would you say the extra hassle is worth it to save memory or should I just just be using writeChars()?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: An Unexpected EOFException

    As long as you are aware of how it works I would just leave it as is. There is no corresponding readChars method so you would have to write your own, passing in a string length.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. EOFException randomly
    By elamre in forum Networking
    Replies: 2
    Last Post: 07-27-2012, 06:20 AM
  2. EOFException
    By Pojahn_M in forum New To Java
    Replies: 1
    Last Post: 03-02-2012, 11:18 AM
  3. java.io.EOFException using readObject
    By yotamoo in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 11:00 AM
  4. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 01:33 PM
  5. EOFException
    By coyne20 in forum Introductions
    Replies: 0
    Last Post: 02-19-2009, 03:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •