码迷,mamicode.com
首页 > 编程语言 > 详细

java 读取文件的字节数组

时间:2016-08-17 18:03:29      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

 

  /*文件64位编码*/

  public static void main(String[] args) {
   byte[] fileByte = toByteArray(newFile);
   String imgStr = new BASE64Encoder().encode(fileByte);
  }

   

/*读取文件的字节数组*/
public
static byte[] toByteArray(File file) throws IOException { File f = file; if (!f.exists()) { throw new FileNotFoundException("file not exists"); } ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(f)); int buf_size = 1024; byte[] buffer = new byte[buf_size]; int len = 0; while (-1 != (len = in.read(buffer, 0, buf_size))) { bos.write(buffer, 0, len); } return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } bos.close(); } }

 

java 读取文件的字节数组

标签:

原文地址:http://www.cnblogs.com/chenweichu/p/5780916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!