Results 1 to 10 of 10
  1. #1
    ZigVe is offline Member
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Rep Power
    0

    Default How can I read a binary file record by record in java?

    I want to fetch a binary file from server and read its records. The binary file is made in Delfy by defining a record type:

    My_Record_Type = packed
    record
    x,y : word;
    a,b; byte;
    end;

    The only way I have found to read this in Java is by wrapping the InputStream with a DataInputStream and read the records by using 2*readChar and 2*readByte in a while loop.

    Is there another easier/faster way to do this? A way of reading record by record like in Delfi?

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default

    What is a "record" in a binary file? In a text file, a record ends with a newline character.
    What defines the end of a record in your binary file?

  3. #3
    ZigVe is offline Member
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Rep Power
    0

    Default

    I am quite new to binary io (and java) so I don't know if Im using "record" in the right way, but in Pascal/Delphi this is called record and in C/C++ I think its called a struct.

    In this case every "record/structure" consists of 6 bytes. Two bytes for "x", two for "y", then one for "a" and the last for "b". Right after the last byte of these 6 bytes, a new record starts. What will be the most efficient way to read this and store it for later use?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default

    What format is the data in?
    For the 2 byte values, what order are the bytes in: High, low or low high?
    Read 6 bytes into an array and pick out the bytes for each variable as per their format.
    Say the bytes are high, low. Something like this:
    int x = (bytes[0]<<8) + (bytes[1] & 0XFF);

  5. #5
    ZigVe is offline Member
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Rep Power
    0

    Default

    The order of the two bytes is little Endian, guess that corresponds to low, high? Should i wrap the InputStream in a BufferedInputStream then or just use the read method of the InputStream? Will this method be faster than using DataInputStream (dis) the following way:

    dis = new DataInputStream (inputStream);
    try{
    while(true){
    int x = littleEndianToBigEndian(dis.readChar()); //as x will hold a value in the range of 0-65535
    int y = littleEndianToBigEndian(dis.readChar()); // y also range 0-65535.
    byte a = readByte();
    byte b = readByte();
    // store the values
    }
    }catch(EOFException e){//end of file brakes while loop}

    Whats the best way of storing the data permanently? Is it faster to read from a FileInputStream than a InputStrem from a url connection? If so I probably should stream directly to file and read the file later? I guess I should mention that I am working on an Android app.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default

    Whats the best way of storing the data permanently?
    Depends on what "best" means? Storage usage. Ease of access. Relations to other data. Persistence. ???
    Is it faster to
    I wouldn't worry about speed until it is a demonstrated problem. How many thousands of these do you plan to read?

  7. #7
    ZigVe is offline Member
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Rep Power
    0

    Default

    I plan to read files of 8-12 kB. I have tested the read method listed above where I store the data in an stringbuffer and display it on screen. It takes about half a minute on a wifi connection to do this and that's a bit slow -therefore I was wondering about speed. The guy that is programming the server side told me to read it record-by-record, but as I didn't know how to do that (or what that meant) in Java, I asked here.

  8. #8
    ZigVe is offline Member
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Rep Power
    0

    Default

    * "listed above" refers to the DataInputStream method. Haven't had the time to try out the method you suggested.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default

    For a speed test, write a simple program that reads the data using each method/class (DataInputStream vs InputStream) and time it using System currentTimeMillis(). Run it several times to even out any OS influences.

    I have no idea what delays reading a file over wifi will have.

  10. #10
    ZigVe is offline Member
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Record Lock in Java
    By sfaisalawan in forum JDBC
    Replies: 1
    Last Post: 05-08-2011, 08:10 AM
  2. pointing to 1st record in a file
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 08:38 AM
  3. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 10:07 PM
  4. Replies: 0
    Last Post: 04-01-2008, 10:43 AM
  5. Replies: 0
    Last Post: 09-28-2007, 01:01 PM

Posting Permissions

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