读取 java 文件到 byte 数组的三种方法 (总结)
paquete zs; import java.io.bufferedInputStream; import java.io.bytearRayOutputStream; import java.io.file; import java.io.fileInputStream; import java.io.filenotfoundException; import java.io.ioxception; import java.io.Randomaccessfile; import java.io.ioxception; import java.io.Randomaccessfile; import java.io.ioxception; import; java.nio.mappedByteBuffer; import java.nio.channels.filechannel; import java.nio.channels.filechannel.mapmode; public class FileUtils {public byte [] getContent (string filePath) lanza ioexception {archivo de archivo = nuevo archivo); larga fileSize = file.ll longitud (); Integer.max_value) {system.out.println ("file demasiado grande ..."); return null;} fileInputStream fi = new FileInputStream (archivo); byte [] buffer = new byte [(int) filesize]; int offset = 0; int numRead = 0; while (offset <buffer.length && (numRead = fi.read (read (lectura (lectura (lectura (lead (reete (lead (lead (lead (lead (Buffer, buffer, buffer, buffer.leng. offset))> = 0) {offset + = numRead;} // 确保所有数据均被读取 if (offset! = buffer.length) {throw new ioException ("no se pudo leer completamente el archivo" + file.getName ());} fi.Close (); return buffer;}/** * el iopsado tradicional * @param filename * @return * @throws ioexception; tobyTearray (string filename) lanza ioexception {archivo f = new File (FileName); if (! F.Exists ()) {tire newenotFoundException (fileName);} byteArArAyAutputStream bos = new ByteArArAyOutputStReam ((int) f.lengther 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 ();} {E.PrintStackTRace (); showe e;} finalmente {try {in.close ();} catch (ioException e) {e.PrintStackTrace ();} bos.Close ();}}}/** * nio Way * * @Param filenam IoException {archivo f = new File (FileName); if (! F.Exists ()) {Throw New FileNotFoundException (FileName);} fileChannel Channel = null; fileInputStream fs = null; try {fs = new FileInputStream (f); canal = fs.getChannel (); byteBuffer byteBuffer = byteBuffer = byteBuffer ((F); canal.size ()); while (((canal.read (byteBuffer))> 0) {// no hacer nada // system.out.println ("Reading");} return byteBuffer.array ();} catch (ioException e) {e.printstacktace (); tirar e;} finalmente {tryin {canal.close ();} Catch (iOexcepte) {E.PrintStackTrace ();} try {fs.Close ();} Catch (ioException e) {e.PrintStackTrace ();}}}/** * Archivo asignado MappedBedBuffer 可以在处理大文件时 可以在处理大文件时 提升性能 * * @param fileName * @return * @throws iOException */public static byte [] toByTeary3 (striet) 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 [(int) fc.size ()]; if (byteBuffer.ReMeining ()> 0) {// System.out.println ("RESTERS"); {fc.close ();} catch (ioException e) {E.PrintStackTrace ();}}}}以上这篇读取 java 文件到 byte 数组的三种方法 (总结) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。 也希望大家多多支持武林网。