Results 1 to 10 of 10
- 07-08-2011, 11:18 AM #1
Member
- Join Date
- Jul 2011
- Location
- Norway
- Posts
- 6
- Rep Power
- 0
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
- 07-08-2011, 01:33 PM #2
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?
- 07-08-2011, 03:25 PM #3
Member
- Join Date
- Jul 2011
- Location
- Norway
- Posts
- 6
- Rep Power
- 0
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?
- 07-08-2011, 03:53 PM #4
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);
- 07-09-2011, 05:00 PM #5
Member
- Join Date
- Jul 2011
- Location
- Norway
- Posts
- 6
- Rep Power
- 0
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.
- 07-09-2011, 05:24 PM #6
Depends on what "best" means? Storage usage. Ease of access. Relations to other data. Persistence. ???Whats the best way of storing the data permanently?
I wouldn't worry about speed until it is a demonstrated problem. How many thousands of these do you plan to read?Is it faster to
- 07-09-2011, 06:52 PM #7
Member
- Join Date
- Jul 2011
- Location
- Norway
- Posts
- 6
- Rep Power
- 0
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.
- 07-09-2011, 06:53 PM #8
Member
- Join Date
- Jul 2011
- Location
- Norway
- Posts
- 6
- Rep Power
- 0
* "listed above" refers to the DataInputStream method. Haven't had the time to try out the method you suggested.
- 07-09-2011, 07:04 PM #9
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.
- 07-09-2011, 07:15 PM #10
Member
- Join Date
- Jul 2011
- Location
- Norway
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Record Lock in Java
By sfaisalawan in forum JDBCReplies: 1Last Post: 05-08-2011, 08:10 AM -
pointing to 1st record in a file
By swati.jyoti in forum New To JavaReplies: 1Last Post: 04-25-2009, 08:38 AM -
Update a record in Random access file
By Rgfirefly24 in forum New To JavaReplies: 2Last Post: 04-24-2008, 10:07 PM -
How to get whole record of a table into a java List
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:43 AM -
How to get whole record of a table into a Java List
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 01:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks