Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File j2me中从二进制文件到字节数组的读取_File_Java Me - Fatal编程技术网

File j2me中从二进制文件到字节数组的读取

File j2me中从二进制文件到字节数组的读取,file,java-me,File,Java Me,我使用以下方法读取二进制文件: 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

我使用以下方法读取二进制文件:

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){}
}
public void readFile()
{
尝试
{
Reader in=new InputStreamReader(this.getClass().getResourceAsStream(this.fileName));
int count=(in.read()*0xFF)+in.read();
int heights=in.read();
this.shapes=newint[count][];

对于(int ii=0;ii,因为您总是处理字节,所以应该使用,而不是

添加Javadoc说明:

InputStreamReader是从字节流到字符流的桥梁:它读取字节并使用指定的字符集将其解码为字符。它使用的字符集可以通过名称指定,也可以显式指定,或者可以接受平台的默认字符集

read()
方法读取一个“字符”:

另一方面,
InputStream
表示字节的输入流:

公共抽象int read()引发IOException

从输入流读取数据的下一个字节。值字节作为0到255范围内的int返回。如果由于到达流的结尾而没有可用的字节,则返回值-1。此方法将阻止,直到输入数据可用、检测到流的结尾或引发异常


(对于kicks,这里有一个)

因为您总是处理字节,所以应该使用,而不是

添加Javadoc说明:

InputStreamReader是从字节流到字符流的桥梁:它读取字节并使用指定的字符集将其解码为字符。它使用的字符集可以通过名称指定,也可以显式指定,或者可以接受平台的默认字符集

read()
方法读取一个“字符”:

另一方面,
InputStream
表示字节的输入流:

公共抽象int read()引发IOException

从输入流读取数据的下一个字节。值字节作为0到255范围内的int返回。如果由于到达流的结尾而没有可用的字节,则返回值-1。此方法将阻止,直到输入数据可用、检测到流的结尾或引发异常


(这里有一个精彩的例子)

我可以直接从诺基亚找到最好的例子

 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;
   }

我可以直接从诺基亚找到最好的例子

 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;
   }