如何将图片,声音,影像等文件读入字节数组中

来源:互联网 发布:java中贪心算法 编辑:程序博客网 时间:2024/05/17 10:06
/**
  * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  */
 publicstaticbyte[] readFileByBytes(String fileName) {
  InputStream in = null;
  ByteArrayOutputStream out = newByteArrayOutputStream();
  try{
   in = newFileInputStream(fileName);
   byte[] buf = newbyte[1024];
   intlength = 0;
   while((length = in.read(buf)) != -1) {
    out.write(buf,0, length);
   }
  }catch(Exception e1) {
   e1.printStackTrace();
  }finally{
   if(in != null) {
    try{
     in.close();
    }catch(IOException e1) {
    }
   }
  }
  returnout.toByteArray();
 }



  1.  /** 
  2.      * NIO way 
  3.      * @param filename 
  4.      * @return 
  5.      * @throws IOException 
  6.      */  
  7.     public static byte[] toByteArray2(String filename)throws IOException{  
  8.           
  9.         File f = new File(filename);  
  10.         if(!f.exists()){  
  11.             throw new FileNotFoundException(filename);  
  12.         }  
  13.           
  14.         FileChannel channel = null;  
  15.         FileInputStream fs = null;  
  16.         try{  
  17.             fs = new FileInputStream(f);  
  18.             channel = fs.getChannel();  
  19.             ByteBuffer byteBuffer = ByteBuffer.allocate((int)channel.size());  
  20.             while((channel.read(byteBuffer)) > 0){  
  21.                 // do nothing  
  22. //              System.out.println("reading");  
  23.             }  
  24.             return byteBuffer.array();  
  25.         }catch (IOException e) {  
  26.             e.printStackTrace();  
  27.             throw e;  
  28.         }finally{  
  29.             try{  
  30.                 channel.close();  
  31.             }catch (IOException e) {  
  32.                 e.printStackTrace();  
  33.             }  
  34.             try{  
  35.                 fs.close();  
  36.             }catch (IOException e) {  
  37.                 e.printStackTrace();  
  38.             }  
  39.         }  
  40.     }  
  41.       
  42.       
  43.     /** 
  44.      * Mapped File  way 
  45.      * MappedByteBuffer 可以在处理大文件时,提升性能 
  46.      * @param filename 
  47.      * @return 
  48.      * @throws IOException 
  49.      */  
  50.     public static byte[] toByteArray3(String filename)throws IOException{  
  51.           
  52.         FileChannel fc = null;  
  53.         try{  
  54.             fc = new RandomAccessFile(filename,"r").getChannel();  
  55.             MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load();  
  56.             System.out.println(byteBuffer.isLoaded());  
  57.             byte[] result = new byte[(int)fc.size()];  
  58.             if (byteBuffer.remaining() > 0) {  
  59. //              System.out.println("remain");  
  60.                 byteBuffer.get(result, 0, byteBuffer.remaining());  
  61.             }  
  62.             return result;  
  63.         }catch (IOException e) {  
  64.             e.printStackTrace();  
  65.             throw e;  
  66.         }finally{  
  67.             try{  
  68.                 fc.close();  
  69.             }catch (IOException e) {  
  70.                 e.printStackTrace();  
  71.             }  
  72.         }  
  73.     }  
  74. }  

0 0