Three methods of reading Java files to a byte array (summary)
Package ZS;
Import Java.io.BufferedInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
Import Java.nio.ByteBuffer;
Import Java.nio.MappedByteBuffer;
Import Java.nio.channels.FileChannel;
Import Java.nio.channels.FileChannel.MapMode;
public class FileUtils {public byte[] getcontent (String filePath) throws IOException {File File = new file (FilePath);
Long fileSize = File.length ();
if (FileSize > Integer.max_value) {System.out.println ("file Too big ...");
return null;
FileInputStream fi = new FileInputStream (file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numread = 0; while (Offset < buffer.length && (Numread = fi.read (buffer, offset, buffer.length-offset)) >= 0) {of
Fset + = Numread; //Ensure that all data is read 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) throws IOException {file F = new file (filename);
if (!f.exists ()) {throw new filenotfoundexception (filename);
Bytearrayoutputstream BOS = new Bytearrayoutputstream ((int) f.length ());
Bufferedinputstream in = null;
try {in = new 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 (buffer, 0, Len);
return Bos.tobytearray ();
catch (IOException e) {e.printstacktrace ();
Throw e;
Finally {try {in.close ();
catch (IOException e) {e.printstacktrace ();
} bos.close (); }/** * NIO way * * @param filename * @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)) > 0) {//Does nothing//System.out.println ("reading");
return Bytebuffer.array ();
catch (IOException e) {e.printstacktrace ();
Throw e;
Finally {try {channel.close ();
catch (IOException e) {e.printstacktrace ();
try {fs.close ();
catch (IOException e) {e.printstacktrace (); /** * Mapped file way Mappedbytebuffer can improve performance when working with large files * * @param filename * @return * @throws ioexce Ption */public static byte[] ToByteArray3 (String filename) throws 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 ("remain");
Bytebuffer.get (result, 0, bytebuffer.remaining ());
return result;
catch (IOException e) {e.printstacktrace ();
Throw e;
Finally {try {fc.close ();
catch (IOException e) {e.printstacktrace ();
}
}
}
}
The above reading Java files to the byte array of three methods (summary) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.