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

来源:互联网 发布:安卓录屏内录声音软件 编辑:程序博客网 时间:2024/06/06 01:42
  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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 火车上突发疾病怎么办 在火车上肚子疼怎么办 户口是学签证怎么办 学生去韩国护照怎么办 户口迁移原护照怎么办 考科四档案丢了怎么办 驾照档案袋丢了怎么办 驾照考试档案丢失怎么办 驾校不给档案怎么办 学员卡丢了怎么办 第一次办身份证急用怎么办 医保卡没有磁性怎么办 退休后大病医保怎么办 退休了医保不够怎么办 社保采集信息后怎么办 医保卡个人欠费怎么办 离婚房产不过户怎么办 科目四五次不过怎么办 科二考试下雨怎么办 科目三无法超车怎么办 下雨天考科目二怎么办 考科二下雨对点怎么办 上海自考准考证过期怎么办 驾照到年龄后怎么办 科三考试约不上怎么办 科目四预约不了怎么办 科目三考试迟到怎么办 科目四无法预约怎么办 新车换年检标志怎么办 科目一二次不过怎么办 科目一两次不过怎么办 考科目一没考过怎么办 科目一挂了怎么办 科目一考试不及格怎么办 科二考试不合格怎么办 驾校没给档案怎么办 考科目一没带身份证怎么办 企业经营期限届满怎么办 网上买票待核验怎么办 手机购票待审核怎么办 护士注册过期了怎么办