读取 Java a 바이트 数组的三种方法 (总结)
패키지 ZS; import java.io.bufferedInputStream; import java.io.bytearRayoutputStream; import java.io.file; import java.io.fileInputStream; import java.io.filenotfoundException; import java.io.io exception; import java.io.randomaccessfile; java.nio.bytebuffer; import java.nio.mappedbytebuffer; import java.nio.channels.filechannel; import java.nio.channels.filechannel.mapmode; public class fileutils {public byte [] getContent (Strows Filepath); 새로운 파일 =); file.length (); if (filesize> integer.max_value) {system.out.println ( "file to big ..."); return null;} fileInputStream fi = new FileInputStream (file); buffer = new Byte [(int) filesize]; int respset = 0; fi.Read (버퍼, 오프셋, 버퍼 .length -Offset))> = 0) {오프셋 + = numread;} // 确保所有数据均被读取 if (offset! = buffer.length) {new ioException ( " + file.getname ());} fi.close (); return way * @param way * @param way * @param way * @param way * @param way * @param way * @param way * @param way * @param filed * @Throws ioException */public static byte [] TobyTearRay (String filename)는 ioexception {file f = new File (filename); if (! f.exists ()) {throw new filenotfoundException (filename);} BiteArrayoutPutStream BOS = 새로운 ByTearRayoutStream (int) f.lengres (int) f.lengrest (int); in = null; try {in = new bufferedInputStream (new fileInputStream (f)); int buf_size = 1024; buffer = new byte [buf_size]; int len = 0; while (leen = in.read (buffer, 0, buf_size))) {bus.wr (buffer, 0, len); bos.tobytearray ();} catch (ioexception e) {e.printstacktrace (); strash e;} 마침내 {in.close ();} catch (ioexception e) {e.printstacktrace ();} bos.close ();}/** * nio way * * @param * @parem * @ @atecome */public static byte [] tobytearRay2 (String filename)는 ioException {file f = new File (filename); if (! f.exists (filename);} filEchannel 채널 = null; fileInputStream fs = null; {fs = new FileinPUTSTREAM (f); f); fs.getChannel (); ByteBuffer bytebuffer = bytebuffer.allocate ((int) 채널. e;} 마지막으로 {try {channel.close ();} catch (ioexcept e) {e.printstacktrace ();} try {fs.close ();} catch (ioexception e) {e.printstacktrace ();}}}}}}}}}}}}}}}}} * @retrow * @param @ @ @ @ @ @ @ @ @ret. ioException */public static byte [] TobyTearRay3 (String filename)은 ioexception {filechannel fc = null; try {fc = new randomaccessfile (filename, "r"). getChannel (); mappedByTebuffer bytebuffer = fc.map (mapMode.Read. 0, fc.size ()). load (); system.out.println (bytebuffer.isloaded ()); byte [] result = new Byte [(int) fc.size ()]; if (bytebuffer.remaining ()> 0) {// system.out.println ( "남아"); bytebuffer.get 0, 0, bytebuffer.remaining ());} return result;} catch (ioexception e) {e.printstacktrace (); throw e;} 마침내 {try {fc.close ();} catch (ioexception e) {e.printstacktrace ();}}}}以上这篇读取 Java a 바이트 数组的三种方法 (总结 总结) 就是小编分享给大家的全部内容了 就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。 也希望大家多多支持武林网。