读取 java 文件到 octet 数组的三种方法 (总结)
Package ZS; Importer java.io.BufferedInputStream; Importer java.io.bytearrayoutputStream; Importer java.io.file; import java.io.fileInputStream; import java.io.filenotfoundException; import java.io.ioException; Importer java.io.randomAccessSfile; import javio.byte; java.nio.mappybytebuffer; import java.nio.channels.filechannel; importer java.nio.channels.filechannel.mapMode; public class fileutils {public byte [] getContent (String Filepath) lance ioException {fichier File = New File (filepath); Longy Integer.max_value) {System.out.println ("File Too Big ..."); retour null;} fileInputStream fi = new FileInputStream (fichier); byte [] buffer = new Byte [(int) fileSize]; int offset = 0; int numread = 0; while (offset <length && (numRead = fi.read (Buffer, Offset.Leteng tamper.length - offset))> = 0) {offset + = numRead;} // 确保所有数据均被读取 if (offset! = buffer.length) {throw new ioException ("n'a pas complètement lu le fichier" + file.getName ());} fi.close (); retour tamper;} / ** * le Way * @Param Filename * @retrn * @Rows Traditional * * @Param filename * @retrn * @hows ioexion * * @Param filename * Byte [] TobyTearray (String FileName) lève ioException {fichier f = nouveau fichier (nom de fichier); if (! f.exists ()) {Throw New FileNotFoundException (nom de fichier);} bytearrayoutputStream bos = new ByteArrayoutStream ((int) f.length ()); buffredInputStream dans = null; in = f.length (); 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 (tampon, 0, len); (IoException e) {e.printStackTrace (); lancer e;} enfin {try {in.close ();} catch (ioException e) {e.printStackTrace ();} bos.close ();}} / ** * Nio Way * * @param filename * @return * @Throws ioException * / public Static By [] ToCyt FileName) lève ioException {fichier f = nouveau fichier (nom de fichier); if (! f.exists ()) {lancer un nouveau filenotFoundException (nom de fichier);} filechannel canal = null; fileInputStream fs = null; try {fs = new FileInputStream (f); channel = fs.getChannel (); bytebuffer bytebuffer = Bytebuffer.ALLOCOT ((int) channel.size ()); while ((channel.read (bytebuffer))> 0) {// ne rien faire // system.out.println ("lecture");} return byteBuffer.Array ();} Catch (ioexception e) {e.printStackTrace (); throw e;} enfin {essaie {canal {););); (IoException e) {e.printStackTrace ();} try {fs.close ();} catch (ioException e) {e.printStackTrace ();}}} / ** * Fichier mappé MaptedByteBuffer 可以在处理大文件时 , 提升性能 * * @param filen * @return * @Throws ioexception * / public Static Byte [] TobyTearray3 (String FileName) lève 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 ("restent"); ByteBuffer.ReMinging ());} Retour Result;} catch (ioException e) {e.printStackTrace (); Throw E;} enfin {try {fc.close ();} catch (ioException e) {e.printStackTrace ();}}}}以上这篇读取 java 文件到 octet 数组的三种方法 (总结) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。