0

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?

2 Answers 2

3

Since you are always dealing with bytes, you should use an InputStream rather than an InputStreamReader.

Add the Javadoc says:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

And the read() method reads a "character":

On the other hand, an InputStream represents an input stream of bytes:

public abstract int read() throws IOException

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

(And for kicks, here's a dated article about "buffered readers" in j2me)

Sign up to request clarification or add additional context in comments.

Comments

0

Best example I could find directly from Nokia

 public Image readFile(String path) {
        try {
            FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
            if(!fc.exists()) {
                System.out.println("File doesn't exist!");
            }
            else {
                int size = (int)fc.fileSize();
                InputStream is = fc.openInputStream();
                byte bytes[] = new byte[size];
                is.read(bytes, 0, size);
                image = Image.createImage(bytes, 0, size);
            }

        } catch (IOException ioe) {
            System.out.println("IOException: "+ioe.getMessage());
        } catch (IllegalArgumentException iae) {
            System.out.println("IllegalArgumentException: "+iae.getMessage());
        }
        return image;
   }

http://www.developer.nokia.com/Community/Wiki/How_to_read_an_image_from_Gallery_in_Java_ME

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.