Java File,object,byte[]間轉換

來源:互聯網
上載者:User
   1. import java.io.BufferedOutputStream;      2. import java.io.ByteArrayInputStream;      3. import java.io.ByteArrayOutputStream;      4. import java.io.File;      5. import java.io.FileInputStream;      6. import java.io.FileOutputStream;      7. import java.io.IOException;      8. import java.io.ObjectInputStream;      9. import java.io.ObjectOutputStream;     10. import java.io.Serializable;     11.     12. public class Byte_File_Object {     13.     14.     /**    15.      * 檔案轉化為位元組數組    16.      * @EditTime 2007-8-13 上午11:45:28    17.      */     18.     public static byte[] getBytesFromFile(File f) {     19.         if (f == null) {     20.             return null;     21.         }     22.         try {     23.             FileInputStream stream = new FileInputStream(f);     24.             ByteArrayOutputStream out = new ByteArrayOutputStream(1000);     25.             byte[] b = new byte[1000];     26.             int n;     27.             while ((n = stream.read(b)) != -1) {    28.                 out.write(b, 0, n);     29.                }    30.             stream.close();     31.             out.close();     32.             return out.toByteArray();     33.         } catch (IOException e) {     34.         }     35.         return null;     36.     }     37.     38.     /**   39.      * 把位元組數組儲存為一個檔案    40.      * @EditTime 2007-8-13 上午11:45:56    41.      */     42.     public static File getFileFromBytes(byte[] b, String outputFile) {     43.         BufferedOutputStream stream = null;     44.         File file = null;     45.         try {     46.             file = new File(outputFile);     47.             FileOutputStream fstream = new FileOutputStream(file);     48.             stream = new BufferedOutputStream(fstream);     49.             stream.write(b);     50.         } catch (Exception e) {     51.             e.printStackTrace();     52.         } finally {     53.             if (stream != null) {     54.                 try {     55.                     stream.close();     56.                 } catch (IOException e1) {     57.                     e1.printStackTrace();     58.                 }     59.             }     60.         }     61.         return file;     62.     }     63.     64.     /**    65.      * 從位元組數組擷取對象    66.      * @EditTime 2007-8-13 上午11:46:34    67.      */     68.     public static Object getObjectFromBytes(byte[] objBytes) throws Exception {     69.         if (objBytes == null || objBytes.length == 0) {     70.             return null;     71.         }     72.         ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);     73.         ObjectInputStream oi = new ObjectInputStream(bi);     74.         return oi.readObject();     75.     }     76.     77.     /**   78.      * 從對象擷取一個位元組數組    79.      * @EditTime 2007-8-13 上午11:46:56    80.      */     81.     public static byte[] getBytesFromObject(Serializable obj) throws Exception {     82.         if (obj == null) {     83.             return null;     84.         }     85.         ByteArrayOutputStream bo = new ByteArrayOutputStream();     86.         ObjectOutputStream oo = new ObjectOutputStream(bo);     87.         oo.writeObject(obj);     88.         return bo.toByteArray();     89.     }     90. }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.