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

原文链接:读取java文件到byte数组的三种方式

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值