您的位置:首页 > 移动开发 > Objective-C

Java File,object,byte[]间转换

2011-12-06 23:35 239 查看
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. }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: