zoukankan      html  css  js  c++  java
  • java 读取文件的字节数组

      /*文件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(); } }
  • 相关阅读:
    解释器
    桥接
    组合
    hbase读性能优化
    Hbase为什么写比读快
    http和https区别
    R apply() 函数和 tapply() 函数
    R 语言 decostand() 函数
    R多行交叉作图
    k-mean 拐点
  • 原文地址:https://www.cnblogs.com/chenweichu/p/5780916.html
Copyright © 2011-2022 走看看