java中序列化与反序列化_Java中的序列化示例
java中序列化与反序列化
Serialization in Java is the process of converting an object into bytes stream to save it in file. Or we can say that, serialization is used to persist (save) the state of an object.
Java中的序列化是将对象转换为字节流以将其保存在文件中的过程。 或者可以说,序列化用于持久化(保存)对象的状态。
The process of recreating object from bytes stream is called deserialization.
从字节流重新创建对象的过程称为反序列化。
Below I have mentioned few applications of serialization in Java.
下面我提到了Java中序列化的一些应用程序。
Uses of Serialization
序列化的用途
To persist the state of an object for future use. To send an object on network. To store user session in web based applications.
保留对象的状态以备将来使用。 在网络上发送对象。 将用户会话存储在基于Web的应用程序中。
To serialize an object, its class must implement Serializable interface.
要序列化一个对象,其类必须实现Serializable接口。
Serializable interface does not have any member and so it is called as marker interface. It tells the Java compiler that the object is serializable.
可序列化的接口没有任何成员,因此被称为标记接口 。 它告诉Java编译器该对象是可序列化的。
To make some data members non serializable, we must declare them as transient.
为了使某些数据成员不可序列化,我们必须将它们声明为瞬时的。
ObjectOutputStream and ObjectInputSteam are two classes that contain methods to serialize and deserialize an object.
ObjectOutputStream和ObjectInputSteam是两个类,它们包含序列化和反序列化对象的方法。
writeObject() method of ObjectOutputStream class is used to write an object to file.
ObjectOutputStream类的writeObject()方法用于将对象写入文件。
readObject() method of ObjectInputStream class is used to read an object from file.
ObjectInputStream类的readObject()方法用于从文件读取对象。
It is a Java convention to give .ser extension to the file in which we are saving the object.
这是Java的约定,将.ser扩展名添加到要保存对象的文件中。
Lets take one example to understand how serialization and deserialization is done in Java.
让我们举一个例子来了解如何在Java中完成序列化和反序列化。
序列化和反序列化示例 (Serialization and Deserialization Example)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import java.io.*;
class Employee implements Serializable {
String name;
int salary;
transient int age;
Employee(String name,int salary,int age) {
this.name=name;
this.salary=salary;
this.age=age;
}
public static void main(String...s) {
Employee e=new Employee("neeraj mishra",50000,21);
try {
FileOutputStream fout=new FileOutputStream("data.ser");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(e);
FileInputStream fin=new FileInputStream("data.ser");
ObjectInputStream in=new ObjectInputStream(fin);
e=(Employee)in.readObject();
System.out.println(e.name);
System.out.println(e.salary);
System.out.println(e.age);
} catch(Exception E) {
E.printStackTrace();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import java . io . * ;
class Employee implements Serializable {
String name ;
int salary ;
transient int age ;
Employee ( String name , int salary , int age ) {
this . name = name ;
this . salary = salary ;
this . age = age ;
}
public static void main ( String . . . s ) {
Employee e = new Employee ( "neeraj mishra" , 50000 , 21 ) ;
try {
FileOutputStream fout = new FileOutputStream ( "data.ser" ) ;
ObjectOutputStream out = new ObjectOutputStream ( fout ) ;
out . writeObject ( e ) ;
FileInputStream fin = new FileInputStream ( "data.ser" ) ;
ObjectInputStream in = new ObjectInputStream ( fin ) ;
e = ( Employee ) in . readObject ( ) ;
System . out . println ( e . name ) ;
System . out . println ( e . salary ) ;
System . out . println ( e . age ) ;
} catch ( Exception E ) {
E . printStackTrace ( ) ;
}
}
}
|
Output
输出量
You can see in above output that value of age variable is 0, which is the default value of integer type variable. This shows that transient data members can’t be serialized. You must also note in above code that when we read the object from file it must be type casted into corresponding class type.
您可以在上面的输出中看到age变量的值为0,这是整数类型变量的默认值。 这表明瞬态数据成员无法序列化。 您还必须在上面的代码中注意,当我们从文件中读取对象时,必须将其类型转换为相应的类类型。
Below I have added a video that will help you to learn concept of serialization in Java. If you found anything incorrect or have any doubts regarding above tutorial then feel free to ask by commenting below.
在下面,我添加了一个视频,它将帮助您学习Java中的序列化概念。 如果您发现任何不正确的地方或对以上教程有任何疑问,请随时在下面的评论中提问。
翻译自: https://www.thecrazyprogrammer.com/2015/10/serialization-in-java-with-example.html
java中序列化与反序列化