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