Is there a more efficient way to break the data in a byte array in Java?
I have written the following function to read a binary file with fixed length data field. But the performance are really slow, I need to read a binary file with 30,000 records each with the length of 300 bytes, and each record contain 240 fields.
Any advise?
public void breakField(byte[] input) {
ByteArrayInputStream bais = new ByteArrayInputStream(input);
byte[] tmp = new byte[2];
bais.read(tmp);
this.id = new String(tmp);
tmp = new byte[4];
bais.read(tmp);
this.name = new String(tmp);
tmp = new byte[8];
bais.read(tmp);
this.phone = new String(tmp);
tmp = new byte[15];
bais.read(tmp);
this.otherInfo = new String(tmp);
.... more fields...
}
By reading the entire file into a
byte[]array, if the file is larger than the amount of space available in memory to the JVM, then you will get anOutOfMemoryError.Instead, you can use a
BufferedReaderin conjunction with aFileReader. This will allow you to read the file piece by piece without loading the entire thing into memory.To load the data record-by-record, simply read in 300 bytes at a time when you call
BufferedReader.read. If you prefer to deal with individual fields, instead of reading 300 bytes at a time, just read the number of bytes that corresponds to the length of the next field.