读取Java文件到byte数组的三种方法(总结)
读取Java文件到byte数组的三种方法(总结)
packagezs;
importjava.io.BufferedInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.RandomAccessFile;
importjava.nio.ByteBuffer;
importjava.nio.MappedByteBuffer;
importjava.nio.channels.FileChannel;
importjava.nio.channels.FileChannel.MapMode;
publicclassFileUtils{
publicbyte[]getContent(StringfilePath)throwsIOException{
Filefile=newFile(filePath);
longfileSize=file.length();
if(fileSize>Integer.MAX_VALUE){
System.out.println("filetoobig...");
returnnull;
}
FileInputStreamfi=newFileInputStream(file);
byte[]buffer=newbyte[(int)fileSize];
intoffset=0;
intnumRead=0;
while(offset<buffer.length
&&(numRead=fi.read(buffer,offset,buffer.length-offset))>=0){
offset+=numRead;
}
//确保所有数据均被读取
if(offset!=buffer.length){
thrownewIOException("Couldnotcompletelyreadfile"
+file.getName());
}
fi.close();
returnbuffer;
}
/**
*thetraditionalioway
*
*@paramfilename
*@return
*@throwsIOException
*/
publicstaticbyte[]toByteArray(Stringfilename)throwsIOException{
Filef=newFile(filename);
if(!f.exists()){
thrownewFileNotFoundException(filename);
}
ByteArrayOutputStreambos=newByteArrayOutputStream((int)f.length());
BufferedInputStreamin=null;
try{
in=newBufferedInputStream(newFileInputStream(f));
intbuf_size=1024;
byte[]buffer=newbyte[buf_size];
intlen=0;
while(-1!=(len=in.read(buffer,0,buf_size))){
bos.write(buffer,0,len);
}
returnbos.toByteArray();
}catch(IOExceptione){
e.printStackTrace();
throwe;
}finally{
try{
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
bos.close();
}
}
/**
*NIOway
*
*@paramfilename
*@return
*@throwsIOException
*/
publicstaticbyte[]toByteArray2(Stringfilename)throwsIOException{
Filef=newFile(filename);
if(!f.exists()){
thrownewFileNotFoundException(filename);
}
FileChannelchannel=null;
FileInputStreamfs=null;
try{
fs=newFileInputStream(f);
channel=fs.getChannel();
ByteBufferbyteBuffer=ByteBuffer.allocate((int)channel.size());
while((channel.read(byteBuffer))>0){
//donothing
//System.out.println("reading");
}
returnbyteBuffer.array();
}catch(IOExceptione){
e.printStackTrace();
throwe;
}finally{
try{
channel.close();
}catch(IOExceptione){
e.printStackTrace();
}
try{
fs.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
/**
*MappedFilewayMappedByteBuffer可以在处理大文件时,提升性能
*
*@paramfilename
*@return
*@throwsIOException
*/
publicstaticbyte[]toByteArray3(Stringfilename)throwsIOException{
FileChannelfc=null;
try{
fc=newRandomAccessFile(filename,"r").getChannel();
MappedByteBufferbyteBuffer=fc.map(MapMode.READ_ONLY,0,
fc.size()).load();
System.out.println(byteBuffer.isLoaded());
byte[]result=newbyte[(int)fc.size()];
if(byteBuffer.remaining()>0){
//System.out.println("remain");
byteBuffer.get(result,0,byteBuffer.remaining());
}
returnresult;
}catch(IOExceptione){
e.printStackTrace();
throwe;
}finally{
try{
fc.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
以上这篇读取Java文件到byte数组的三种方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。