Java提供了将对象以字节的形式写入到磁盘中,在需要的时候在从磁盘中恢复的功能,在网络传输中非常有用,下面将代码贴上,分别用两种形式实现了对象的序列化,其中第二种当时能将指定的变量序列化,更加人性化的随心所欲
[java] view plaincopyprint?
01.package com.bird.thinking;
02.
03.import java.io.FileInputStream;
04.import java.io.FileOutputStream;
05.import java.io.ObjectInputStream;
06.import java.io.ObjectOutputStream;
07.import java.io.Serializable;
08./**
09. * @use 实现对对象的序列化和反序列化
10. * @author Bird
11. *
12. */
13.public class Data {
14. public static void writeObject() throws Exception{
15. Worm worm = new Worm("Bird");
16. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d://worm.out"));
17. out.writeObject(worm);
18. out.close();//关闭的同时也刷新清空了缓冲区
19. }
20.
21.
22. public static void readObject() throws Exception{
23. ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://worm.out"));
24. Worm s2 = (Worm) in.readObject();
25. System.out.println(s2);
26. }
27.
28. public static void main(String[] args) throws Exception{
29. // writeObject();
30. readObject();
31. }
32.}
33.
34.
35.class Worm implements Serializable{
36.
37. private static final long serialVersionUID = 1L;
38. private String name = null;
39. public Worm(String name){
40. this.name = name;
41. }
42.
43. public String toString(){
44. return name;
45. }
46.}
package com.bird.thinking;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* @use 实现对对象的序列化和反序列化
* @author Bird
*
*/
public class Data {
public static void writeObject() throws Exception{
Worm worm = new Worm("Bird");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d://worm.out"));
out.writeObject(worm);
out.close();//关闭的同时也刷新清空了缓冲区
}
public static void readObject() throws Exception{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://worm.out"));
Worm s2 = (Worm) in.readObject();
System.out.println(s2);
}
public static void main(String[] args) throws Exception{
// writeObject();
readObject();
}
}
class Worm implements Serializable{
private static final long serialVersionUID = 1L;
private String name = null;
public Worm(String name){
this.name = name;
}
public String toString(){
return name;
}
}
下面是第二种方式
[java] view plaincopyprint?
01.package com.bird.thinking;
02.
03.import java.io.Externalizable;
04.import java.io.FileInputStream;
05.import java.io.FileNotFoundException;
06.import java.io.FileOutputStream;
07.import java.io.IOException;
08.import java.io.ObjectInput;
09.import java.io.ObjectInputStream;
10.import java.io.ObjectOutput;
11.import java.io.ObjectOutputStream;
12./**
13. * @use 第二种可控制序列化变量个数的方式
14. * @author Bird
15. *
16. */
17.public class Blip3 implements Externalizable {
18. private int i;
19. private String s;//没有实例化
20. public Blip3(){
21. System.out.println("Blip3 Constructor!!");
22. }
23. //注意 这里的s没有实例化
24.
25. public Blip3(String x, int a){
26. System.out.println("Blip3(String x, int a)");
27. s = x;
28. i = a;
29. //s 和 i实例化在非默认构造函数中
30. }
31.
32. public String toString(){
33. return s + i;
34. }
35.
36. public void writeExternal(ObjectOutput out){//可选择写入变量
37. System.out.println("Blip3.writeExternal");
38. try {
39. out.writeObject(s);
40. out.writeInt(i);
41. } catch (IOException e) {
42. throw new RuntimeException(e);
43. }//必须进行这两项,否则就一个变量也不出初始化
44. }
45.
46. public void readExternal(ObjectInput in){//可选择读入数据
47. System.out.println("Blip3.readExternal");
48. try {
49. s = (String)in.readObject();
50. i = in.readInt();
51. } catch (ClassNotFoundException e) {
52.
53. } catch (IOException e) {
54. throw new RuntimeException(e);
55. }
56.
57. }
58.
59. public void read() throws FileNotFoundException, IOException, ClassNotFoundException{//读取序列化的类
60. ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://Blip3.out"));
61. System.out.println("Revovering b3");
62. Blip3 b3 = (Blip3)in.readObject();
63. System.out.println(b3);
64. }
65.
66. public void write() throws Exception{//写入对象
67. Blip3 b3 = new Blip3("A String", 47);
68. System.out.println(b3);
69. ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("d://Blip3.out"));
70. System.out.println("Saving Object");
71. o.writeObject(b3);
72. o.close();
73. }
74.
75. public static void main(String[] args) throws Exception{
76. Blip3 b = new Blip3();
77. // b.write();
78. b.read();
79. }
80.}
package com.bird.thinking;
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
* @use 第二种可控制序列化变量个数的方式
* @author Bird
*
*/
public class Blip3 implements Externalizable {
private int i;
private String s;//没有实例化
public Blip3(){
System.out.println("Blip3 Constructor!!");
}
//注意 这里的s没有实例化
public Blip3(String x, int a){
System.out.println("Blip3(String x, int a)");
s = x;
i = a;
//s 和 i实例化在非默认构造函数中
}
public String toString(){
return s + i;
}
public void writeExternal(ObjectOutput out){//可选择写入变量
System.out.println("Blip3.writeExternal");
try {
out.writeObject(s);
out.writeInt(i);
} catch (IOException e) {
throw new RuntimeException(e);
}//必须进行这两项,否则就一个变量也不出初始化
}
public void readExternal(ObjectInput in){//可选择读入数据
System.out.println("Blip3.readExternal");
try {
s = (String)in.readObject();
i = in.readInt();
} catch (ClassNotFoundException e) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void read() throws FileNotFoundException, IOException, ClassNotFoundException{//读取序列化的类
ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://Blip3.out"));
System.out.println("Revovering b3");
Blip3 b3 = (Blip3)in.readObject();
System.out.println(b3);
}
public void write() throws Exception{//写入对象
Blip3 b3 = new Blip3("A String", 47);
System.out.println(b3);
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("d://Blip3.out"));
System.out.println("Saving Object");
o.writeObject(b3);
o.close();
}
public static void main(String[] args) throws Exception{
Blip3 b = new Blip3();
// b.write();
b.read();
}
}
输出结果为
[html] view plaincopyprint?
01.Blip3 Constructor!!
02.Blip3(String x, int a)
03.A String47
04.Saving Object
05.Blip3.writeExternal
Blip3 Constructor!!
Blip3(String x, int a)
A String47
Saving Object
Blip3.writeExternal
[html] view plaincopyprint?
01.Blip3 Constructor!!
02.Revovering b3
03.Blip3 Constructor!!
04.Blip3.readExternal
//哥们,够详细了吧,希望对你有帮助。{:soso_e100:}
05.A String47
|