File tree Expand file tree Collapse file tree
main/java/com/baeldung/serialization
test/java/com/baeldung/serialization Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeuldung .serialization ;
2+ import java .io .Serializable ;
3+
4+ public class Person implements Serializable {
5+
6+ /**
7+ *
8+ */
9+ private static final long serialVersionUID = 1L ;
10+
11+ private int age ;
12+ private String name ;
13+
14+ public int getAge () {
15+ return age ;
16+ }
17+
18+ public void setAge (int age ) {
19+ this .age = age ;
20+ }
21+
22+ public String getName () {
23+ return name ;
24+ }
25+
26+ public void setName (String name ) {
27+ this .name = name ;
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .baeuldung .serialization ;
2+ import static org .junit .Assert .assertEquals ;
3+ import static org .junit .Assert .assertTrue ;
4+
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+
11+ import org .junit .Test ;
12+
13+ public class PersonTest {
14+
15+ @ Test
16+ public void testPeron () throws IOException , ClassNotFoundException {
17+ Person p = new Person ();
18+ p .setAge (20 );
19+ p .setName ("Joe" );
20+
21+ FileOutputStream fileOutputStream = new FileOutputStream ("yofile.txt" );
22+ ObjectOutputStream objectOutputStream = new ObjectOutputStream (fileOutputStream );
23+ objectOutputStream .writeObject (p );
24+ objectOutputStream .flush ();
25+ objectOutputStream .close ();
26+
27+ FileInputStream fileInputStream = new FileInputStream ("yofile.txt" );
28+ ObjectInputStream objectInputStream = new ObjectInputStream (fileInputStream );
29+ Person p2 = (Person ) objectInputStream .readObject ();
30+ objectInputStream .close ();
31+ assertTrue (p2 .getAge () == p .getAge ());
32+ assertTrue (p2 .getName ().equals (p .getName ()));
33+ }
34+
35+ }
You can’t perform that action at this time.
0 commit comments