zoukankan      html  css  js  c++  java
  • JAVA中文件与Byte数组相互转换

    public class FileUtil {

    //将文件转换成Byte数组
    public static byte[] getBytesByFile(String pathStr) {
    File file = new File(pathStr);
    try {
    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
    byte[] b = new byte[1000];
    int n;
    while ((n = fis.read(b)) != -1) {
    bos.write(b, 0, n);
    }
    fis.close();
    byte[] data = bos.toByteArray();
    bos.close();
    return data;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }

    //将Byte数组转换成文件
    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
    File dir = new File(filePath);
    if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
    dir.mkdirs();
    }
    file = new File(filePath + "\" + fileName);
    fos = new FileOutputStream(file);
    bos = new BufferedOutputStream(fos);
    bos.write(bytes);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (bos != null) {
    try {
    bos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (fos != null) {
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
  • 相关阅读:
    Swift如何判断上午还是下午
    Qt Creator编译app到iPhone
    用swift判断string是否包含字母
    QToolTip显示富文本问题
    mac如何发起屏幕共享?
    Redis持久化
    bean 实例化原理解析
    WebSocket和SocketIO总结
    netty入门
    redis 工具类
  • 原文地址:https://www.cnblogs.com/fengquan-blog/p/14840249.html
Copyright © 2011-2022 走看看