Java之序列化Object对象和字节数组或文件之间的转换

来源:互联网 发布:淘宝关键词在哪里设置 编辑:程序博客网 时间:2024/05/01 01:57

 Java中File,byte[],Object间的转换

 1、Object 对象必须是可序列化对象 。 
 2、可序列化的 Object 对象都可以转换为一个磁盘文件;反过来则不一定成立,只有序列化文件才可以转换为Object对象。

 

示例:

[java] view plain copy
 print?
  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. }   
感谢http://blog.csdn.net/android_robot/article/details/6774325



0 0
原创粉丝点击