I use below method for reading a binary file :
public void readFile()
{
try
{
Reader in = new InputStreamReader( this.getClass().getResourceAsStream( this.fileName));
int count = (in.read() * 0xFF) + in.read();
int heights = in.read();
this.shapes = new int[count][];
for(int ii = 0;ii<count;ii++)
{
int gwidth = in.read();
int[] tempG = new int[gwidth * heights];
int len = (in.read() * 0xff) + in.read();
for(int jj = 0;jj<len;jj++)
{
tempG[top++] = in.read() * 0x1000000;
}
this.shapes[ii] = tempG;
}
in.close();
}catch(Exception e){}
}
It works perfectly in netbeans emulator and some devices,but in some devices and in kemulator it seems that in.read(), read a char (two byte), and it causes my app crashes on those device and emulator.
what is the best method for reading file in bytes?
Since you are always dealing with bytes, you should use an
InputStreamrather than anInputStreamReader.Add the Javadoc says:
And the
read()method reads a “character”:On the other hand, an
InputStreamrepresents an input stream of bytes:(And for kicks, here’s a dated article about “buffered readers” in j2me)