读取Java文件到byte数组的三种方法(总结)

作者:jingxian 时间:2023-08-01 17:19:39 

读取Java文件到byte数组的三种方法(总结)


package zs;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class FileUtils {
public byte[] getContent(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}

/**
* the traditional io way
*
* @param filename
* @return
* @throws IOException
*/
public static byte[] toByteArray(String filename) throws IOException {

File f = new File(filename);
if (!f.exists()) {
throw new FileNotFoundException(filename);
}

ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(f));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
}

/**
* NIO way
*
* @param filename
* @return
* @throws IOException
*/
public static byte[] toByteArray2(String filename) throws IOException {

File f = new File(filename);
if (!f.exists()) {
throw new FileNotFoundException(filename);
}

FileChannel channel = null;
FileInputStream fs = null;
try {
fs = new FileInputStream(f);
channel = fs.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
while ((channel.read(byteBuffer)) > 0) {
// do nothing
// System.out.println("reading");
}
return byteBuffer.array();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
*
* @param filename
* @return
* @throws IOException
*/
public static byte[] toByteArray3(String filename) throws IOException {

FileChannel fc = null;
try {
fc = new RandomAccessFile(filename, "r").getChannel();
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
fc.size()).load();
System.out.println(byteBuffer.isLoaded());
byte[] result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
// System.out.println("remain");
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
标签:java,文件,byte,数组
0
投稿

猜你喜欢

  • C# WPF如何反射加载Geometry几何图形数据图标

    2021-09-06 11:39:08
  • 在Android中通过Intent使用Bundle传递对象的使用方法

    2023-04-23 10:15:38
  • 微信小程序 springboot后台如何获取用户的openid

    2023-01-13 17:07:42
  • Android中Parcelable的作用实例解析

    2022-08-07 09:19:32
  • Java设计模式之状态模式

    2022-05-08 07:24:25
  • android studio3.0以上如何通过navicat访问SQLite数据库文件

    2022-03-26 14:28:55
  • Java ShutdownHook原理详解

    2023-11-10 21:30:36
  • android 仿微信聊天气泡效果实现思路

    2022-11-12 21:11:35
  • Unity C#打包AssetBundle与场景详解

    2022-02-19 14:04:13
  • Eclipse+Java+Swing实现学生成绩管理系统的实例代码

    2022-12-14 15:35:04
  • Android提高之SQLite分页读取实现方法

    2022-11-28 22:44:04
  • C语言实现简单弹跳小球

    2022-07-03 12:54:34
  • C#探秘系列(一)——ToDictionary,ToLookup

    2023-04-19 09:16:15
  • C# 通过同步和异步实现优化做早餐的时间

    2022-10-27 23:09:27
  • Android如何使用Bmob后端云实现失物招领功能

    2023-10-15 07:28:05
  • SpringBoot项目为何引入大量的starter?如何自定义starter?

    2022-12-28 21:39:02
  • Android实现截屏方式整理(总结)

    2023-12-07 05:10:18
  • Java之Spring注解开发案例详解

    2022-05-23 05:33:02
  • 关于统计数字问题的算法

    2023-11-03 00:33:32
  • Java中类的加载顺序执行结果

    2022-03-12 18:12:35
  • asp之家 软件编程 m.aspxhome.com