欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-13 19:36:33
读取java文件到byte数组的三种方法(总结) package zs; import java.io.bufferedinputstream; impo...

读取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数组的三种方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。