字节数组、文件以及Object之间的转换

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. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值