java 读取文件成字节数组_java读取文件为字节数组

将文件读取为字节数组,可用与读取图片,在生成图片

1.[代码][Java]代码

InputStream is = null;

ByteArrayOutputStream out = new ByteArrayOutputStream();

try {

is = new FileInputStream(pathStr);// pathStr 文件路径

byte[] b = new byte[1024];

int n;

while ((n = is.read(b)) != -1) {

out.write(b, 0, n);

}// end while

} catch (Exception e) {

log.error(getText("TimingMmsService.error") + e.getMessage());

throw new Exception("System error,SendTimingMms.getBytesFromFile", e);

} finally {

if (is != null) {

try {

is.close();

} catch (Exception e) {

log.error(e);// TODO

}// end try

}// end if

}// end try

return out.toByteArray();

本文链接:https://my.lmcjl.com/post/14603.html

展开阅读全文

4 评论

留下您的评论.