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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package zs;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.IOException;  
  9. import java.io.RandomAccessFile;  
  10. import java.nio.ByteBuffer;  
  11. import java.nio.MappedByteBuffer;  
  12. import java.nio.channels.FileChannel;  
  13. import java.nio.channels.FileChannel.MapMode;  
  14.   
  15. public class FileUtils {  
  16.     public byte[] getContent(String filePath) throws IOException {  
  17.         File file = new File(filePath);  
  18.         long fileSize = file.length();  
  19.         if (fileSize > Integer.MAX_VALUE) {  
  20.             System.out.println("file too big...");  
  21.             return null;  
  22.         }  
  23.         FileInputStream fi = new FileInputStream(file);  
  24.         byte[] buffer = new byte[(int) fileSize];  
  25.         int offset = 0;  
  26.         int numRead = 0;  
  27.         while (offset < buffer.length  
  28.         && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {  
  29.             offset += numRead;  
  30.         }  
  31.         // 确保所有数据均被读取  
  32.         if (offset != buffer.length) {  
  33.         throw new IOException("Could not completely read file "  
  34.                     + file.getName());  
  35.         }  
  36.         fi.close();  
  37.         return buffer;  
  38.     }  
  39.   
  40.     /** 
  41.      * the traditional io way 
  42.      *  
  43.      * @param filename 
  44.      * @return 
  45.      * @throws IOException 
  46.      */  
  47.     public static byte[] toByteArray(String filename) throws IOException {  
  48.   
  49.         File f = new File(filename);  
  50.         if (!f.exists()) {  
  51.             throw new FileNotFoundException(filename);  
  52.         }  
  53.   
  54.         ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());  
  55.         BufferedInputStream in = null;  
  56.         try {  
  57.             in = new BufferedInputStream(new FileInputStream(f));  
  58.             int buf_size = 1024;  
  59.             byte[] buffer = new byte[buf_size];  
  60.             int len = 0;  
  61.             while (-1 != (len = in.read(buffer, 0, buf_size))) {  
  62.                 bos.write(buffer, 0, len);  
  63.             }  
  64.             return bos.toByteArray();  
  65.         } catch (IOException e) {  
  66.             e.printStackTrace();  
  67.             throw e;  
  68.         } finally {  
  69.             try {  
  70.                 in.close();  
  71.             } catch (IOException e) {  
  72.                 e.printStackTrace();  
  73.             }  
  74.             bos.close();  
  75.         }  
  76.     }  
  77.   
  78.     /** 
  79.      * NIO way 
  80.      *  
  81.      * @param filename 
  82.      * @return 
  83.      * @throws IOException 
  84.      */  
  85.     public static byte[] toByteArray2(String filename) throws IOException {  
  86.   
  87.         File f = new File(filename);  
  88.         if (!f.exists()) {  
  89.             throw new FileNotFoundException(filename);  
  90.         }  
  91.   
  92.         FileChannel channel = null;  
  93.         FileInputStream fs = null;  
  94.         try {  
  95.             fs = new FileInputStream(f);  
  96.             channel = fs.getChannel();  
  97.             ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());  
  98.             while ((channel.read(byteBuffer)) > 0) {  
  99.                 // do nothing  
  100.                 // System.out.println("reading");  
  101.             }  
  102.             return byteBuffer.array();  
  103.         } catch (IOException e) {  
  104.             e.printStackTrace();  
  105.             throw e;  
  106.         } finally {  
  107.             try {  
  108.                 channel.close();  
  109.             } catch (IOException e) {  
  110.                 e.printStackTrace();  
  111.             }  
  112.             try {  
  113.                 fs.close();  
  114.             } catch (IOException e) {  
  115.                 e.printStackTrace();  
  116.             }  
  117.         }  
  118.     }  
  119.   
  120.     /** 
  121.      * Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能 
  122.      *  
  123.      * @param filename 
  124.      * @return 
  125.      * @throws IOException 
  126.      */  
  127.     public static byte[] toByteArray3(String filename) throws IOException {  
  128.   
  129.         FileChannel fc = null;  
  130.         try {  
  131.             fc = new RandomAccessFile(filename, "r").getChannel();  
  132.             MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,  
  133.                     fc.size()).load();  
  134.             System.out.println(byteBuffer.isLoaded());  
  135.             byte[] result = new byte[(int) fc.size()];  
  136.             if (byteBuffer.remaining() > 0) {  
  137.                 // System.out.println("remain");  
  138.                 byteBuffer.get(result, 0, byteBuffer.remaining());  
  139.             }  
  140.             return result;  
  141.         } catch (IOException e) {  
  142.             e.printStackTrace();  
  143.             throw e;  
  144.         } finally {  
  145.             try {  
  146.                 fc.close();  
  147.             } catch (IOException e) {  
  148.                 e.printStackTrace();  
  149.             }  
  150.         }  
  151.     }  
  152. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值