首页 > 其他 > 详细

java io ---文件读取为byte数组

时间:2014-02-09 22:57:10      阅读:397      评论:0      收藏:0      [点我收藏+]

直接上代码

/**
  * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  */
 public static byte[] readFileByBytes(String fileName) {
  InputStream in = null;
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
   in = new FileInputStream(fileName);
   byte[] buf = new byte[1024];
   int length = 0;
   while ((length = in.read(buf)) != -1) {
    out.write(buf, 0, length);
   }
  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException e1) {
    }
   }
  }
  return out.toByteArray();
 }


java io ---文件读取为byte数组

原文:http://blog.csdn.net/smile0198/article/details/19015027

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!