欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 文件和byte互轉(zhuǎn)的實例

 更新時間:2017年11月16日 09:15:20   作者:BeforeYou  
下面小編就為大家分享一篇java 文件和byte互轉(zhuǎn)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

實例如下所示:

/** 
  * 獲得指定文件的byte數(shù)組 
  */ 
 private byte[] getBytes(String filePath){ 
  byte[] buffer = null; 
  try { 
   File file = new File(filePath); 
   FileInputStream fis = new FileInputStream(file); 
   ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); 
   byte[] b = new byte[1000]; 
   int n; 
   while ((n = fis.read(b)) != -1) { 
    bos.write(b, 0, n); 
   } 
   fis.close(); 
   bos.close(); 
   buffer = bos.toByteArray(); 
  } catch (FileNotFoundException e) { 
   e.printStackTrace(); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
  return buffer; 
 }
 
 /** 
  * 根據(jù)byte數(shù)組,生成文件 
  */ 
 public static void getFile(byte[] bfile, String filePath,String fileName) { 
  BufferedOutputStream bos = null; 
  FileOutputStream fos = null; 
  File file = null; 
  try { 
   File dir = new File(filePath); 
   if(!dir.exists()&&dir.isDirectory()){//判斷文件目錄是否存在 
    dir.mkdirs(); 
   } 
   file = new File(filePath+"\\"+fileName); 
   fos = new FileOutputStream(file); 
   bos = new BufferedOutputStream(fos); 
   bos.write(bfile); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } finally { 
   if (bos != null) { 
    try { 
     bos.close(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
   } 
   if (fos != null) { 
    try { 
     fos.close(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
   } 
  } 
 }

以上這篇java 文件和byte互轉(zhuǎn)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論