You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 文件操作工具类
*
* 核心职责:提供文件与字节数组之间的转换功能
*
* 技术特点:
* 1. 基于Java IO流实现文件读取
* 2. 使用缓冲区提高读取效率
* 3. 支持大文件的分块读取
* 4. 自动资源管理(需要改进)
*
* 使用场景:
* - 文件上传前的数据准备
* - 文件内容读取为二进制数据
* - 文件存储到数据库的BLOB字段
* - 文件传输和网络通信
*
* @author
* @version 1.0
*/
public class FileUtil {
/**
* 将文件转换为字节数组
*
* @param file 要转换的文件对象
* @return 文件的字节数组表示
* @throws IOException 当文件读取失败时抛出IO异常
*
* 方法说明:
* 1. 通过文件输入流读取文件内容
* 2. 使用字节数组输出流缓存数据
* 3. 采用缓冲区分块读取,避免内存溢出
* 4. 返回完整的文件字节数组
*/
public static byte[] FileToByte(File file) throws IOException {
// 创建文件输入流,用于读取文件内容
@SuppressWarnings("resource") // 抑制资源未关闭警告(需要改进)
InputStream content = new FileInputStream(file);
// 创建字节数组输出流,用于缓存读取的数据
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
// 创建缓冲区每次读取100字节
byte[] buff = new byte[100];
int rc = 0; // 实际读取的字节数
// 循环读取文件内容,直到文件末尾
while ((rc = content.read(buff, 0, 100)) > 0) {
// 将读取到的数据写入字节数组输出流
swapStream.write(buff, 0, rc);
}
// 获得完整的二进制数组并返回
return swapStream.toByteArray();
}
// 可以添加的增强方法:
/**
* 增强版文件转字节数组使用try-with-resources自动关闭流
*
* @param file 要转换的文件对象
* @return 文件的字节数组
* @throws IOException 当文件读取失败时抛出IO异常
*/
public static byte[] fileToByteSafe(File file) throws IOException {
// 使用try-with-resources确保流正确关闭
try (FileInputStream content = new FileInputStream(file);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream()) {
// 使用更大的缓冲区提高读取效率8KB
byte[] buff = new byte[8192];
int rc = 0;
// 循环读取文件内容
while ((rc = content.read(buff)) != -1) {
swapStream.write(buff, 0, rc);
}
return swapStream.toByteArray();
}
}
/**
* 文件转字节数组(指定缓冲区大小)
*
* @param file 要转换的文件对象
* @param bufferSize 缓冲区大小(字节)
* @return 文件的字节数组
* @throws IOException 当文件读取失败时抛出IO异常
*/
public static byte[] fileToByte(File file, int bufferSize) throws IOException {
try (FileInputStream content = new FileInputStream(file);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream()) {
byte[] buff = new byte[bufferSize];
int rc = 0;
while ((rc = content.read(buff)) != -1) {
swapStream.write(buff, 0, rc);
}
return swapStream.toByteArray();
}
}
/**
* 获取文件大小(字节数)
*
* @param file 文件对象
* @return 文件大小(字节)
*/
public static long getFileSize(File file) {
if (file.exists() && file.isFile()) {
return file.length();
}
return 0;
}
/**
* 验证文件是否存在且可读
*
* @param file 文件对象
* @return 文件是否可读
*/
public static boolean isFileReadable(File file) {
return file != null && file.exists() && file.isFile() && file.canRead();
}
/**
* 将字节数组保存为文件
*
* @param data 字节数组
* @param file 目标文件
* @throws IOException 当文件写入失败时抛出IO异常
*/
public static void byteToFile(byte[] data, File file) throws IOException {
try (java.io.FileOutputStream fos = new java.io.FileOutputStream(file)) {
fos.write(data);
fos.flush();
}
}
}