![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package myDemo; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.ObjectInputStream; 10 import java.io.ObjectOutputStream; 11 import java.io.Serializable; 12 import java.util.ArrayList; 13 import java.util.HashMap; 14 import java.util.Iterator; 15 import java.util.List; 16 import java.util.Map; 17 import java.util.Scanner; 18 import java.util.Stack; 19 import java.io.BufferedReader; 20 public class Test implements Serializable { 21 private String name; 22 private int age; 23 public Test(String name,int age){ 24 this.age=age; 25 this.name=name; 26 27 } 28 public String getName(){ 29 return name; 30 } 31 public int getAge(){ 32 return age; 33 } 34 35 public static void main(String[] args) throws IOException, ClassNotFoundException 36 { 37 Test t=new Test("zhangshan",20); 38 File f=new File("serializableDemo.txt"); 39 System.out.print("haha"); 40 if(!f.exists()){ 41 f.createNewFile(); 42 43 } 44 System.out.println(f.getAbsolutePath()); 45 FileOutputStream out=new FileOutputStream(f); 46 ObjectOutputStream oout=new ObjectOutputStream(out); 47 oout.writeObject(t); 48 oout.close(); 49 out.close(); 50 FileInputStream in=new FileInputStream(f); 51 ObjectInputStream inn=new ObjectInputStream(in); 52 Test a=(Test)inn.readObject(); 53 System.out.println(a.getAge()); 54 System.out.println(a.getName()); 55 } 56 57 58 }