直接上代码
/** * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */ 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(); }
原文:http://blog.csdn.net/smile0198/article/details/19015027