pull/2/head
zxy 4 months ago
parent b2152c234c
commit e046888abb

@ -7,21 +7,33 @@ import java.io.IOException;
import java.io.InputStream;
/**
* :
*/
* :
*/
public class FileUtil {
/**
*
*
* @param file
* @return
* @throws IOException I/O
*/
public static byte[] FileToByte(File file) throws IOException {
// 将数据转为流
// 将文件数据转输入,这里使用 FileInputStream 来读取文件内容
@SuppressWarnings("resource")
InputStream content = new FileInputStream(file);
// 创建一个 ByteArrayOutputStream 用于存储从文件中读取的数据
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
// 创建一个字节缓冲区,用于每次从输入流中读取数据
byte[] buff = new byte[100];
// 用于存储每次读取的字节数
int rc = 0;
// 循环读取文件内容直到读取完所有数据read 方法返回 -1 表示读取到文件末尾)
while ((rc = content.read(buff, 0, 100)) > 0) {
// 将读取到的字节写入到 ByteArrayOutputStream 中
swapStream.write(buff, 0, rc);
}
// 获得二进制数组
// 将 ByteArrayOutputStream 中的数据转换为字节数组并返回
return swapStream.toByteArray();
}
}
}
Loading…
Cancel
Save