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

来源:互联网 发布:中企网络通信 项目经理 编辑:程序博客网 时间:2024/06/05 09:44

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

18856人阅读 评论(0)收藏举报
本文章已收录于:
分类:
作者同类文章X
    作者同类文章X
      [java] view plain copy
      print?在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. }  


      2
      0
       
       

      我的同类文章

      http://blog.csdn.net
      • String.getBytes()2016-05-21
      • JAVA中Long与Integer比较容易犯的错误2016-05-14
      • JAVA获取同一路径下所有子类或接口实现类2016-05-04
      • 在spring的bean中注入内部类2016-02-01
      • JAVA字符串格式化-String.format()的使用2015-07-28
      • 用java获取本机IP地址2015-02-02
      • Java double 相乘的小问题记录2016-05-19
      • 深入理解Java:SimpleDateFormat安全的时间格式化2016-05-12
      • ServiceLoader的使用2016-05-04
      • Java 输出日历格式2015-08-06
      • JAVA中Collections.sort()实现List排序的公共方法和自定义方法2015-07-08
      更多文章
      0 0