public class Dog implements Serializable
String name ;
public Dog(String name)
this.name = name ;
public static void main(String args[])
Dog dog = new Dog("crazy");
//建立保存对象的文件
File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test.obj");
if(file.exists())
file.delete();
try
file.createNewFile();
catch (IOException e)
e.printStackTrace();
//使用ObjectOutputStream
ObjectOutputStream out = null ;
try
/*
ObjectOutputStream将对象变成流,然后利用FileOutputStream写到文件里面。
因此,很容易想到,也可以利用socket的inputStream进行进程间的通信
*/
out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(dog);
out.flush();
catch (IOException e)
e.printStackTrace();
finally
try
out.close();
catch (IOException e)
e.printStackTrace();
执行代码后得到一个test.obj文件,但是里面是乱码:
2.1.2 反序列化代码
public class Dog implements Serializable
String name ;
public Dog(String name)
this.name = name ;
public static void main(String args[])
//取得保存序列化的文件
File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test.obj");
//使用ObjectInputStream流来进行读入
ObjectInputStream in = null ;
try
//从FileInputStream流里面读出数据,然后将其转换成对象
in = new ObjectInputStream(new FileInputStream(file));
Dog dog = (Dog) in.readObject();
System.out.println(dog.name);
catch (IOException e)
e.printStackTrace();
catch (ClassNotFoundException e)
e.printStackTrace();
finally
try
in.close();
catch (IOException e)
e.printStackTrace();
public class Dog implements Serializable
static final long serialVersionUID = 2016041901 ;
String name ;
public Dog(String name)
this.name = name ;
public static void main(String args[])
//取得保存序列化的文件
File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test.obj");
//使用ObjectInputStream流来进行读入
ObjectInputStream in = null ;
try
//从FileInputStream流里面读出数据,然后将其转换成对象
in = new ObjectInputStream(new FileInputStream(file));
Dog dog = (Dog) in.readObject();
System.out.println(dog.name);
catch (IOException e)
e.printStackTrace();
catch (ClassNotFoundException e)
e.printStackTrace();
finally
try
in.close();
catch (IOException e)
e.printStackTrace();
运行结果:
java.io.InvalidClassException: com.example.BaseKnowledge.Dog; local class incompatible: stream classdesc serialVersionUID = 3079980272438232802, local class serialVersionUID = 2016041901
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:560)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1580)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1493)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1729)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
at com.example.BaseKnowledge.Dog.main(Dog.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
public class Main
public static void main(String args[])
try
ServerSocket serverSocket = new ServerSocket(10000);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Dog dog = null ;
dog = (Dog) objectInputStream.readObject();
System.out.println(dog.name);
catch (ClassNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
发送端代码:
public class Dog implements Serializable
static final long serialVersionUID = 2016041901 ;
String name ;
public Dog(String name)
this.name = name ;
public static void main(String args[])
ObjectOutputStream out = null ;
try
Socket socket = new Socket("127.0.0.1",10000);
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(new Dog("hello"));
out.flush();
catch (IOException e)
e.printStackTrace();
finally
try
out.close();
catch (IOException e)
e.printStackTrace();