读取java文件到byte
パッケージzs; java.io.bufferedinputStream; Import java.io.bytearrayoutputStream; Import java.io.file; Import java.io.fileinputStream; Import java.io.fileenotfoundexception; Import java.io.ioexception; import java.io.randomacessfile.bby.bby.bby.bby.bby.bby. java.nio.mappbytebuffer; Import java.nio.channels.filechannel; Import java.nio.channels.filechannel.mapmode; public class fileutils {public byte [] getContent(] getContent(String filepath)Throws ioException {fileize = fileize> lengize> lengize> lengize> lengeize> lengeize> lengeze integer.max_value){system.out.println( "ファイルが大きすぎる..."); return null;} fileinputStream fi = new fileinputStream(file); buffer = new byte [(int)filesize]; int offset = 0; int numread = 0; offset)> = 0){offset + = numread;} // tobytearray(string filename)throws ioexception {file f = new file(filename); if(!f.exists()){throw new filenotfoundexception(filename);} bytearrayoutputStream = new bytearrayoutputStream((int)f.lentemtrem; new new new new new new new new new new new new new new new new buff((int)f.lent) 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); {e.printstacktrace(); throw e;} fullly {try {in.close();} catch(ioexception e){e.printstacktrace();} bos.close();}/** * nio way * * @param filename * @throws ioexception */public buytearray [filtearray] 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 = fseguffer.alteBuffer.alteber.alteber.alteber( channel.size()); while((channel.read(bytebuffer))> 0){// do Nothing // system.out.println( "reading");} return bytebuffer.array();} catch(ioexception e){e.printstacktrace(); throw E {e.printstacktrace();} try {fs.close();} catch(ioexception e){e.printstacktrace();}}}/** *マッピングbytebuffer ioException {felechannel fc = null; try {fc = new RandomAccessFile(filename、 "r")。getChannel(); mappedbytebuffer bytebuffer = fc.map(mapmode.read_only、0、fc.size())。 new byte [(int)fc.size()]; if(bytebuffer.remaining()> 0){// system.out.println( "remain"); bytebuffer.get(result、0、bytebuffer.remaining();} return result result;} catch(ioexception e){e.printstacktrace e show es(); {fc.close();} catch(ioexception e){e.printstacktrace();}}}}}}}以上这篇读取java文件到byte数组的三种方法(总结)就是小编分享给大家的全部内容了、希望能给大家一个参考、也希望大家多多支持武林网。