您的位置:首页 > 编程语言

读取文件转换成字节数组的代码

2009-07-29 11:28 573 查看
 

     
public byte[] getFile(String location){

FileConnection fc = null;

ByteArrayOutputStream bStrm = null;

InputStream is;

try{

fc = (FileConnection)Connector.open(location, Connector.READ);

//Check if it is more than 150kb, if so throw an exception

if((int)fc.fileSize() > 64000){

showInfo( "files too big " );

}

is = fc.openInputStream();

int ch;

bStrm = new ByteArrayOutputStream();

while ((ch = is.read()) != -1){

bStrm.write(ch);

}

return bStrm.toByteArray();

}catch(Exception e){
showInfo( "get files error " );
return null;
}finally{

if(fc != null){try{fc.close();}catch(Exception ee){}}

if(bStrm != null){try{bStrm.close();}catch(Exception ee){}}

}

}
      
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  exception null string byte
相关文章推荐