29.Serializable
发表日期:2022-08-05 16:41:10 | 来源: | | 浏览(793) 分类:JAVA基础
Pet
01 | import java.io.Externalizable; |
02 | import java.io.IOException; |
03 | import java.io.ObjectInput; |
04 | import java.io.ObjectOutput; |
05 |
06 |
07 | public class Pet implements Externalizable{ |
08 | |
09 | private static final long serialVersionUID = 1L; |
10 | private String name ; |
11 | private int age ; |
12 | private String sex ; |
13 |
14 | public Pet(){} |
15 | |
16 | public Pet(String name , int age , String sex){ |
17 | setName(name); |
18 | setAge(age); |
19 | setSex(sex); |
20 | } |
21 |
22 | public String toString() { |
23 | return "姓名:" +name+ " 年龄:" +age+ " 性别:" +sex; |
24 | } |
25 | |
26 | @Override |
27 | public void readExternal(ObjectInput in) throws IOException, |
28 | ClassNotFoundException { |
29 | // TODO Auto-generated method stub |
30 | setName((String) in.readObject()); |
31 | setAge(in.readInt()); |
32 | //setSex((String) in.readObject()); Sex就不会被保存 |
33 | } |
34 |
35 | @Override |
36 | public void writeExternal(ObjectOutput out) throws IOException { |
37 | // TODO Auto-generated method stub |
38 | out.writeObject(getName()); |
39 | out.writeInt(getAge()); |
40 | //out.writeObject(getSex()); |
41 | } |
42 |
43 | public void setName(String name) { |
44 | this .name = name; |
45 | } |
46 |
47 | public String getName() { |
48 | return name; |
49 | } |
50 |
51 | public void setAge( int age) { |
52 | this .age = age; |
53 | } |
54 |
55 | public int getAge() { |
56 | return age; |
57 | } |
58 |
59 | public void setSex(String sex) { |
60 | this .sex = sex; |
61 | } |
62 |
63 | public String getSex() { |
64 | return sex; |
65 | } |
66 |
67 | } |
Person
01 | import java.io.Serializable; |
02 |
03 |
04 | public class Person implements Serializable{ |
05 |
06 | /** |
07 | * |
08 | */ |
09 | private static final long serialVersionUID = 1L; |
10 | |
11 | private String name ; |
12 | private int age ; |
13 | private transient String sex ; //transient 声明的变量不会被序列号 |
14 | |
15 | public Person(String name, int age , String sex) { |
16 | // TODO Auto-generated constructor stub |
17 | setName(name); |
18 | setAge(age); |
19 | setSex(sex); |
20 | } |
21 |
22 | public String toString() { |
23 | return "姓名:" +name+ " 年龄:" +age+ " 性别:" +sex; |
24 | } |
25 |
26 | public void setName(String name) { |
27 | this .name = name; |
28 | } |
29 | |
30 | public String getName() { |
31 | return name; |
32 | } |
33 | |
34 | public void setAge( int age) { |
35 | this .age = age; |
36 | } |
37 | |
38 | public int getAge() { |
39 | return age; |
40 | } |
41 |
42 | public void setSex(String sex) { |
43 | this .sex = sex; |
44 | } |
45 |
46 | public String getSex() { |
47 | return sex; |
48 | } |
49 | |
50 |
51 | } |
ObjectOutputStreamDemo
01 | import java.io.File; |
02 | import java.io.FileInputStream; |
03 | import java.io.FileOutputStream; |
04 | import java.io.IOException; |
05 | import java.io.InputStream; |
06 | import java.io.ObjectInputStream; |
07 | import java.io.ObjectOutputStream; |
08 | import java.io.OutputStream; |
09 |
10 |
11 | public class ObjectOutputStreamDemo { |
12 |
13 | /** |
14 | * @param args |
15 | * @throws IOException |
16 | * @throws ClassNotFoundException |
17 | */ |
18 | public static void main(String[] args) throws IOException, ClassNotFoundException { |
19 | // TODO Auto-generated method stub |
20 |
21 | File file = new File( "D:" + File.separator + "javademo.txt" ); |
22 |
23 |
24 | OutputStream out = new FileOutputStream(file); |
25 | ObjectOutputStream oos = new ObjectOutputStream(out); |
26 | oos.writeObject( new Person( "张三" , 22 , "男" )); //性别不会被保存 transient 声明的变量不会被序列号 |
27 |
28 |
29 | InputStream in = new FileInputStream(file); |
30 | ObjectInputStream ois = new ObjectInputStream(in); |
31 | Person person = (Person) ois.readObject(); |
32 | System.out.println(person.getName()); |
33 | System.out.println(person.getAge()); |
34 | System.out.println(person); |
35 |
36 | } |
37 |
38 | } |
ObjectOutputStreamDemo2
01 | import java.io.File; |
02 | import java.io.FileInputStream; |
03 | import java.io.FileOutputStream; |
04 | import java.io.IOException; |
05 | import java.io.InputStream; |
06 | import java.io.ObjectInputStream; |
07 | import java.io.ObjectOutputStream; |
08 | import java.io.OutputStream; |
09 |
10 |
11 | public class ObjectOutputStreamDemo2 { |
12 |
13 | /** |
14 | * @param args |
15 | * @throws IOException |
16 | * @throws ClassNotFoundException |
17 | */ |
18 | public static void main(String[] args) throws IOException, ClassNotFoundException { |
19 | // TODO Auto-generated method stub |
20 | |
21 | File file = new File( "D:" +File.separator+ "javademo.txt" ); |
22 | |
23 | |
24 | OutputStream out = new FileOutputStream(file); |
25 | ObjectOutputStream oos = new ObjectOutputStream(out ); |
26 | oos.writeObject( new Pet( "小白" , 3 , "公" )); //性别不会被保存 没有配置writeExternal / readExternal |
27 | |
28 | |
29 | InputStream in = new FileInputStream(file); |
30 | ObjectInputStream ois = new ObjectInputStream(in); |
31 | Pet pet = (Pet) ois.readObject(); |
32 | System.out.println(pet.getName()); |
33 | System.out.println(pet.getAge()); |
34 | System.out.println(pet); |
35 |
36 | } |
37 |
38 | } |
- JAVA(0)
- JAVA基础(30)
- 1.JAVA开发环境配置(0)
- 2.java数据类型(0)
- 3.数组(0)
- 4.Date(0)
- 5.String 和 StringBuffer类常用方法(0)
- 6.Math类(0)
- 7.Cloneable(0)
- 8.File 文件(0)
- 9.FileReader和FileWriter(0)
- 10.RandomAccessFile(0)
- 11.FileInputStream和FileOutputStream(0)
- 12.InputStreamReader和OutputStreamWriter(0)
- 13.BufferedReader(0)
- 14.Scanner(0)
- 15.DataOutputStream(0)
- 16.Thead 多线程(0)
- 17.TimerTask(0)
- 18.zip(0)
- 19.Charset(0)
- 20.List(0)
- 21.Map(0)
- 22.Properties(0)
- 23.Enumeration(0)
- 24.Collection(0)
- 25.JDBC(0)
- 26.Iterator(0)
- 27.Abstract(0)
- 28.Interface(0)
- 29.Serializable(0)
- 30.Camparable(0)
- JSP基础(50)