Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49d36b9

Browse files
another serialization example
1 parent0cdd340 commit49d36b9

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagelearnHeadFirstJava.another_serialization_example;
2+
3+
importjava.io.FileInputStream;
4+
importjava.io.IOException;
5+
importjava.io.ObjectInputStream;
6+
7+
publicclassDeseriliazeDemo {
8+
privatestaticfinalStringFILE_DIRECTORY ="/tmp/employee.ser";
9+
staticEmployeee =null;
10+
publicstaticvoidmain(String[]args) {
11+
try {
12+
FileInputStreamfis =newFileInputStream(FILE_DIRECTORY);
13+
ObjectInputStreamois =newObjectInputStream(fis);
14+
e = (Employee)ois.readObject();
15+
ois.close();
16+
fis.close();
17+
}catch (IOExceptione) {
18+
System.out.println("Caught IOException");
19+
e.printStackTrace();
20+
return;
21+
}catch (ClassNotFoundExceptione) {
22+
System.out.println("Employee class not found");
23+
e.printStackTrace();
24+
return;
25+
}
26+
27+
System.out.println("Deserialized employee.ser is: " +e.toString() +".");
28+
}
29+
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packagelearnHeadFirstJava.another_serialization_example;
2+
3+
importjava.io.Serializable;
4+
5+
importorg.springframework.stereotype.Component;
6+
importorg.springframework.stereotype.Repository;
7+
importorg.springframework.stereotype.Service;
8+
9+
importlombok.Getter;
10+
importlombok.RequiredArgsConstructor;
11+
importlombok.Setter;
12+
13+
//Any one of the following three annotations: @Service, @Repository and @Component will work fine here since I'm doing a component-scan.
14+
//@Component
15+
//@Repository
16+
@Service
17+
@RequiredArgsConstructor
18+
publicclassEmployeeimplementsSerializable {
19+
@Setter
20+
@Getter
21+
privateStringaddress;
22+
@Setter
23+
@Getter
24+
privateStringname;
25+
@Setter
26+
@Getter
27+
privateintage;
28+
@Setter
29+
@Getter
30+
privatedoublesalary;
31+
@Setter
32+
@Getter
33+
privatetransientintSSN;// any field that cannot be serialized needs to be
34+
// marked as transient, for demo purpose, we
35+
// just set SSN as a non-serializable field, it
36+
// doesn't mean anything, it's just that we
37+
// don't want to serialize it.
38+
39+
publicvoidmailCheck() {
40+
System.out.println("Mailing a check to " +name +" at " +address);
41+
}
42+
43+
@Override
44+
publicStringtoString() {//this toString() method doesn't contain SSN field either.
45+
return"Employee [address=" +address +", name=" +name +", age="
46+
+age +", salary=" +salary +"]";
47+
}
48+
49+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
packagelearnHeadFirstJava.another_serialization_example;
2+
3+
importjava.io.FileInputStream;
4+
importjava.io.FileOutputStream;
5+
importjava.io.IOException;
6+
importjava.io.ObjectInputStream;
7+
importjava.io.ObjectOutputStream;
8+
9+
importorg.springframework.context.support.AbstractApplicationContext;
10+
importorg.springframework.context.support.ClassPathXmlApplicationContext;
11+
12+
/**
13+
* This is a combo class that combines the other two classes together:
14+
* SerializeDemo and DeserializeDemo, makes it easier to read and play with.
15+
*/
16+
publicclassMainApp {
17+
privatestaticfinalStringFILE_DIRECTORY ="/tmp/employee.ser";
18+
19+
publicstaticvoidmain(String[]args) {
20+
AbstractApplicationContextcontext =newClassPathXmlApplicationContext(
21+
"spring-configuration/serialization-spring-configuration.xml");
22+
Employeeemployee = (Employee)context.getBean("employee");
23+
24+
// Employee employee = new Employee();//I don't need this line any more,
25+
// since I quickly set up a component-scan using Spring to auto wire
26+
// this employee bean for me, this is so cool! I feel so at home with
27+
// Spring dependency injection now! Praise the Lord!a
28+
employee.setName("Steve Sun");
29+
employee.setAge(26);
30+
employee.setAddress("1860 Charmeran Ave, San Jose, USA. 2015/08/08");
31+
employee.setSSN(12345678);
32+
employee.setSalary(103000);
33+
34+
try {
35+
FileOutputStreamfos =newFileOutputStream(FILE_DIRECTORY);
36+
ObjectOutputStreamous =newObjectOutputStream(fos);
37+
ous.writeObject(employee);
38+
ous.close();
39+
fos.close();
40+
System.out.println("Serialized data is saved in " +FILE_DIRECTORY
41+
+".");
42+
}catch (IOExceptione) {
43+
e.printStackTrace();
44+
}
45+
46+
Employeeemp =null;
47+
48+
try {
49+
FileInputStreamfis =newFileInputStream(FILE_DIRECTORY);
50+
ObjectInputStreamois =newObjectInputStream(fis);
51+
emp = (Employee)ois.readObject();
52+
ois.close();
53+
fis.close();
54+
}catch (IOExceptione) {
55+
System.out.println("Caught IOException");
56+
e.printStackTrace();
57+
return;
58+
}catch (ClassNotFoundExceptione) {
59+
System.out.println("Employee class not found");
60+
e.printStackTrace();
61+
return;
62+
}
63+
64+
System.out.println("Deserialized employee.ser is: " +emp.toString()
65+
+".");
66+
}
67+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
packagelearnHeadFirstJava.another_serialization_example;
2+
3+
importjava.io.FileOutputStream;
4+
importjava.io.IOException;
5+
importjava.io.ObjectOutputStream;
6+
7+
importorg.springframework.context.support.AbstractApplicationContext;
8+
importorg.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
/**
11+
* Java provides a mechanism, called object serialization where an object can be
12+
* represented as a sequence of bytes that includes the object's data as well as
13+
* information about the object's type and the types of data stored in the
14+
* object.
15+
*
16+
* After a serialized object has been written into a file, it can be read from
17+
* the file and deserialized that is, the type information and bytes that
18+
* represent the object and its data can be used to recreate the object in
19+
* memory.
20+
*
21+
* Most impressive is that the entire process is JVM independent, meaning an
22+
* object can be serialized on one platform and deserialized on an entirely
23+
* different platform.
24+
*
25+
* Classes ObjectInputStream and ObjectOutputStream are high-level streams that
26+
* contain the methods for serializing and deserializing an object.
27+
*
28+
* The ObjectOutputStream class contains many write methods for writing various
29+
* data types, but one method in particular stands out:
30+
*
31+
* public final void writeObject(Object x) throws IOException
32+
*
33+
* The above method serializes an Object and sends it to the output stream.
34+
* Similarly, the ObjectInputStream class contains the following method for
35+
* deserializing an object:
36+
*
37+
* public final Object readObject() throws IOException, ClassNotFoundException
38+
*
39+
* This method retrieves the next Object out of the stream and deserializes it.
40+
* The return value is Object, so you will need to cast it to its appropriate
41+
* data type.
42+
*/
43+
44+
publicclassSerializeDemo {
45+
46+
privatestaticfinalStringFILE_DIRECTORY ="/tmp/employee.ser";
47+
48+
publicstaticvoidmain(String[]args) {
49+
AbstractApplicationContextcontext =newClassPathXmlApplicationContext(
50+
"spring-configuration/serialization-spring-configuration.xml");
51+
Employeeemployee = (Employee)context.getBean("employee");
52+
53+
// Employee employee = new Employee();//I don't need this line any more,
54+
// since I quickly set up a component-scan using Spring to auto wire
55+
// this employee bean for me, this is so cool! I feel so at home with
56+
// Spring dependency injection now! Praise the Lord!a
57+
employee.setName("Steve Sun");
58+
employee.setAge(26);
59+
employee.setAddress("1860 Charmeran Ave, San Jose, USA.");
60+
employee.setSSN(12345678);
61+
employee.setSalary(103000);
62+
63+
try {
64+
FileOutputStreamfos =newFileOutputStream(FILE_DIRECTORY);
65+
ObjectOutputStreamous =newObjectOutputStream(fos);
66+
ous.writeObject(employee);
67+
ous.close();
68+
fos.close();
69+
System.out.println("Serialized data is saved in " +FILE_DIRECTORY
70+
+".");
71+
}catch (IOExceptione) {
72+
e.printStackTrace();
73+
}
74+
}
75+
76+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beansxmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10+
11+
<context:component-scanbase-package="learnHeadFirstJava.another_serialization_example"/>
12+
13+
</beans>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp