读取Java文件到byte数组的三种方法(总结)
程序员文章站
2024-03-13 14:59: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数组的三种方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: Java中的装箱和拆箱深入理解
推荐阅读
-
读取Java文件到byte数组的三种方法(总结)
-
JAVA中读取文件(二进制,字符)内容的几种方法总结
-
Java中byte输出write到文件的实现方法讲解
-
java 读取文件方法的总结
-
java读取文件内容的三种方法代码片断分享(java文件操作)
-
Java中byte输出write到文件的实现方法讲解
-
java读取文件内容的三种方法代码片断分享(java文件操作)
-
paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结
-
paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结
-
php读取文件内容到数组的方法