文件转Byte[]、以及byte[]转文件的工具类

本文详细介绍了一种在Java中实现文件与字节数组(byte[])相互转换的方法。包括使用File类、FileInputStream和FileOutputStream等API的具体步骤,以及如何通过自定义工具类简化这一过程。适用于需要在网络上传输文件或在内存中处理文件内容的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、
1.文件转Byte[]:

		// 第1步、使用File类找到一个文件
        File f= new File("e:/demo/demoNext" + File.separator + "java.docx") ;    // 声明File对象
        // 第2步、通过子类实例化父类对象
        InputStream input = null ;    // 准备好一个输入的对象
        input = new FileInputStream(f)  ;    // 通过对象多态性,进行实例化
        // 第3步、进行读操作
        byte result[] = new byte[1024] ;        // 所有的内容都读到此数组之中
        input.read(result) ;        // 读取内容   网络编程中 read 方法会阻塞
        // 第4步、关闭输出流
        input.close() ;                        // 关闭输出流
        System.out.println("内容为:" + result);

2.文件转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(1024);
            byte[] b = new byte[1024];
            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;
    }
    }

测试类调用:

		String pathStr="e:/demo/demoNext/java.docx";
        System.out.println("文件转byte:"+fileUtil.getBytesByFile(pathStr));

二、byte[]转文件的工具类:

public class fileUtil {
	//将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();
                }
            }
        }
    }
}

测试类调用:

		//要生成文件的路径
		String filePath="e:/demo";
		//要生成文件的名字
        String fileName="java.docx";
        //result:byte[]
        fileUtil.getFileByBytes(result, filePath, fileName);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟驿站ㅤ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值