Java byte[]方式读写文件(终极实现)

代码由来

程序员写代码读写文件是最基本的操作。尤其是读写byte[],因为所有类型的文件归根结底都是byte[]。但是很多人写的代码往往只是简单的实现功能,既不考虑代码执行效率,也不考虑代码的美观程度。所以我写下这篇博文记录下到目前为止我所知道的最美观简洁高效的读取文件的代码,分享出来供新人参考。

如果这篇博客帮助到了你,你可给它点个赞,这将是对我莫大的鼓励,谢谢!

代码

Java Files(Since Java7)

摘抄自廖雪峰老师的Java教程

// 读取、写入字节数组
byte[] data = Files.readAllBytes(Paths.get("/path/to/file.txt"));
Files.write(Paths.get("/path/to/file.txt"), data);

// 读取、写入字符 Since:Java11
// 默认使用UTF-8编码读取:
String content1 = Files.readString(Paths.get("/path/to/file.txt"));
// 可指定编码:
String content2 = Files.readString(Paths.get("/path/to/file.txt"), StandardCharsets.ISO_8859_1);
// 按行读取并返回每行内容:
List<String> lines = Files.readAllLines(Paths.get("/path/to/file.txt"));

// 写入文本并指定编码: Since:Java11
Files.writeString(Paths.get("/path/to/file.txt"), "文本内容...", StandardCharsets.ISO_8859_1);
// 按行写入文本:
Files.write(Paths.get("/path/to/file.txt"), lines);

JDK1.6以前的实现

	/**
	 * 以byte[]方式读取文件
	 * 
	 * @param fileName 文件名
	 * @return 文件字节数组
	 * @throws IOException
	 */
	public static byte[] readFileByBytes(String fileName) throws IOException {
		InputStream in = null;
		ByteArrayOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(fileName));
			out = new ByteArrayOutputStream();
			byte[] tempbytes = new byte[in.available()];
			for (int i = 0; (i = in.read(tempbytes)) != -1;) {
				out.write(tempbytes, 0, i);
			}
		} finally {
			if (in != null) {
				in.close();
			}
		}
		return out.toByteArray();
	}

	/**
	 * 向文件写入byte[]
	 * 
	 * @param fileName 文件名
	 * @param bytes    字节
	 * @param append   是否追加
	 * @throws IOException
	 */
	public static void writeFileByBytes(String fileName, byte[] bytes, boolean append) throws IOException {
		OutputStream out = null;
		try {
			out = new BufferedOutputStream(new FileOutputStream(fileName, append));
			out.write(bytes);
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 从文件开头向文件写入byte[]
	 * 
	 * @param fileName 文件名
	 * @param bytes    字节
	 * @throws IOException
	 */
	public static void writeFileByBytes(String fileName, byte[] bytes) throws IOException {
		writeFileByBytes(fileName, bytes, false);
	}

JDK1.7以后的实现

	/**
	 * 以byte[]方式读取文件
	 * 
	 * @param fileName 文件名
	 * @return	文件字节数组
	 * @throws IOException
	 */
	public static byte[] readFileByBytes(String fileName) throws IOException {
		try (InputStream in = new BufferedInputStream(new FileInputStream(fileName)); 
			 ByteArrayOutputStream out = new ByteArrayOutputStream();) {
			byte[] tempbytes = new byte[in.available()];
			for (int i = 0; (i = in.read(tempbytes)) != -1;) {
				out.write(tempbytes, 0, i);
			}
			return out.toByteArray();
		}
	}

	/**
	 * 向文件写入byte[]
	 * 
	 * @param fileName 文件名
	 * @param bytes    字节内容
	 * @param append   是否追加
	 * @throws IOException
	 */
	public static void writeFileByBytes(String fileName, byte[] bytes, boolean append) throws IOException {
		try(OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName, append))){
			out.write(bytes);
		}
	}

	/**
	 * 从文件开头向文件写入byte[]
	 * 
	 * @param fileName 文件名
	 * @param bytes    字节
	 * @throws IOException
	 */
	public static void writeFileByBytes(String fileName, byte[] bytes) throws IOException {
		writeFileByBytes(fileName, bytes, false);
	}
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值