parent
ba84eacd63
commit
5edba550fe
@ -0,0 +1,22 @@
|
|||||||
|
package com.utils; // 声明包路径
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream; // 导入字节数组输出流类
|
||||||
|
import java.io.File; // 导入文件操作类
|
||||||
|
import java.io.FileInputStream; // 导入文件输入流类
|
||||||
|
import java.io.IOException; // 导入IO异常类
|
||||||
|
import java.io.InputStream; // 导入输入流基类
|
||||||
|
|
||||||
|
public class FileUtil { // 文件工具类定义
|
||||||
|
|
||||||
|
public static byte[] FileToByte(File file) throws IOException { // 文件转字节数组方法
|
||||||
|
@SuppressWarnings("resource") // 忽略资源未关闭警告
|
||||||
|
InputStream content = new FileInputStream(file); // 创建文件输入流
|
||||||
|
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); // 创建字节数组输出流
|
||||||
|
byte[] buff = new byte[100]; // 创建100字节缓冲区
|
||||||
|
int rc = 0; // 读取字节数变量
|
||||||
|
while ((rc = content.read(buff, 0, 100)) > 0) { // 循环读取文件内容
|
||||||
|
swapStream.write(buff, 0, rc); // 将读取内容写入输出流
|
||||||
|
}
|
||||||
|
return swapStream.toByteArray(); // 返回字节数组
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue