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

来源:互联网 发布:计算机专业除了编程 编辑:程序博客网 时间:2024/05/22 14:59

直接上代码

/**  * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。  */ 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(); }


2 0
原创粉丝点击