读取Java文件到byte数组的三种方式

来源:互联网 发布:打数字软件 编辑:程序博客网 时间:2024/06/01 07:27
  1. 代码如下

  2. import java.io.BufferedInputStream;  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.IOException;  
  8. import java.io.RandomAccessFile;  
  9. import java.nio.ByteBuffer;  
  10. import java.nio.MappedByteBuffer;  
  11. import java.nio.channels.FileChannel;  
  12. import java.nio.channels.FileChannel.MapMode;  
  13.   
  14.   
  15. public class FileUtils {  
  16.   
  17.     /** 
  18.      * the traditional io way  
  19.      * @param filename 
  20.      * @return 
  21.      * @throws IOException 
  22.      */  
  23.     public static byte[] toByteArray(String filename) throws IOException{  
  24.           
  25.         File f = new File(filename);  
  26.         if(!f.exists()){  
  27.             throw new FileNotFoundException(filename);  
  28.         }  
  29.   
  30.         ByteArrayOutputStream bos = new ByteArrayOutputStream((int)f.length());  
  31.         BufferedInputStream in = null;  
  32.         try{  
  33.             in = new BufferedInputStream(new FileInputStream(f));  
  34.             int buf_size = 1024;  
  35.             byte[] buffer = new byte[buf_size];  
  36.             int len = 0;  
  37.             while(-1 != (len = in.read(buffer,0,buf_size))){  
  38.                 bos.write(buffer,0,len);  
  39.             }  
  40.             return bos.toByteArray();  
  41.         }catch (IOException e) {  
  42.             e.printStackTrace();  
  43.             throw e;  
  44.         }finally{  
  45.             try{  
  46.                 in.close();  
  47.             }catch (IOException e) {  
  48.                 e.printStackTrace();  
  49.             }  
  50.             bos.close();  
  51.         }  
  52.     }  
  53.       
  54.       
  55.     /** 
  56.      * NIO way 
  57.      * @param filename 
  58.      * @return 
  59.      * @throws IOException 
  60.      */  
  61.     public static byte[] toByteArray2(String filename)throws IOException{  
  62.           
  63.         File f = new File(filename);  
  64.         if(!f.exists()){  
  65.             throw new FileNotFoundException(filename);  
  66.         }  
  67.           
  68.         FileChannel channel = null;  
  69.         FileInputStream fs = null;  
  70.         try{  
  71.             fs = new FileInputStream(f);  
  72.             channel = fs.getChannel();  
  73.             ByteBuffer byteBuffer = ByteBuffer.allocate((int)channel.size());  
  74.             while((channel.read(byteBuffer)) > 0){  
  75.                 // do nothing  
  76. //              System.out.println("reading");  
  77.             }  
  78.             return byteBuffer.array();  
  79.         }catch (IOException e) {  
  80.             e.printStackTrace();  
  81.             throw e;  
  82.         }finally{  
  83.             try{  
  84.                 channel.close();  
  85.             }catch (IOException e) {  
  86.                 e.printStackTrace();  
  87.             }  
  88.             try{  
  89.                 fs.close();  
  90.             }catch (IOException e) {  
  91.                 e.printStackTrace();  
  92.             }  
  93.         }  
  94.     }  
  95.       
  96.       
  97.     /** 
  98.      * Mapped File  way 
  99.      * MappedByteBuffer 可以在处理大文件时,提升性能 
  100.      * @param filename 
  101.      * @return 
  102.      * @throws IOException 
  103.      */  
  104.     public static byte[] toByteArray3(String filename)throws IOException{  
  105.           
  106.         FileChannel fc = null;  
  107.         try{  
  108.             fc = new RandomAccessFile(filename,"r").getChannel();  
  109.             MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load();  
  110.             System.out.println(byteBuffer.isLoaded());  
  111.             byte[] result = new byte[(int)fc.size()];  
  112.             if (byteBuffer.remaining() > 0) {  
  113. //              System.out.println("remain");  
  114.                 byteBuffer.get(result, 0, byteBuffer.remaining());  
  115.             }  
  116.             return result;  
  117.         }catch (IOException e) {  
  118.             e.printStackTrace();  
  119.             throw e;  
  120.         }finally{  
  121.             try{  
  122.                 fc.close();  
  123.             }catch (IOException e) {  
  124.                 e.printStackTrace();  
  125.             }  
  126.         }  
  127.     }  
  128. }  
1 0