读取 java 文件到 byte 数组的三种方法 (总结)
paket zs; impor java.io.bufferedInputStream; impor java.io.bytearrayoutputStream; impor java.io.file; impor java.io.fileinputStream; impor java.io.filenotfoundException; impor java.io.ioException; impor java.io.io.io.io. java.nio.bytebuffer; import java.nio.mappedbytebuffer; import java.nio.channels.filechannel; import file {public {public {public {public {public {public byte [] getContent (string file appath); file.length (); if (filessize> integer.max_value) {System.out.println ("File terlalu besar ..."); return null;} fileInputStream fi = FileInputStream baru (file); byte [] buffer = byte baru (int) fileSize]; int offset = 0; byte [] buffer = byte baru [int) fileSize]; int offset = 0; 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) melempar ioException {file f = file baru (fileName); if (! f.exists ()) {throw baru filenotfoundException (fileName);} bytearRayoutOutstream bos = bos = bos = bos =e bos = bos =teRear (filen); f.length ()); bufferedInputStream in = null; coba {in = new bufferedInputStream (FileInputStream baru (f)); int buf_size = 1024; byte [] buffer = byte baru [buf_size]; int len = 0; while (-1! = (len = in.read, bufer, BUFFER, 0; while (-1! = (len = in.read, Buffer, BUFFER, 0; 0, len);} return bos.tobytearray ();} catch (ioException e) {e.printstacktrace (); lempar e;} akhirnya {coba {in.close ();} catch (ioException e) {e.printstackrace ();} bos. @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);} return bye (}} {// byte. {E.PrintStackTrace (); throw e;} akhirnya {try {channel.close ();} catch (ioException e) {e.printstacktrace ();} coba {fs.close ();}} ~ , ~ {ioException e) {e.printstack (); @param fileName * @return * @throws ioException */public static byte [] tobytearray3 (string filename) melempar ioException {filechannel fc = null; coba {fc = new acakAccessFile (filename, "r"). getchannel (); mappedbybuffer (fileneon, "r"). getchannel (); mappedbybuffer (fileneon, "r"). getchannel (); mappedbybuffer (fileneon, "r"). getchannel (); mappedbybuffer (fileneon, "r"). 0, fc.size ()). Load (); System.out.println (byteBuffer.isloaded ()); byte [] hasil = byte baru [(int) fc.size ()]; if (bytebuffer.remaining ()> 0) {// System.out.println ("tetap"); byte () {// System.out.println ("tetap"); byteBuffer.remaining ());} return result;} catch (ioException e) {e.printstacktrace (); lempar e;} akhirnya {coba {fc.close ();}}}}}}}} {e.printStackRace ();以上这篇读取 Java 文件到 byte 数组的三种方法 (总结) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。