请参阅下面给出的代码。希望这能解决您的问题:
package com.shineed.io;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
public class Deserializer{
public static void main (String args[]) {
Deserializer deserializer = new Deserializer();
Customer customer = deserializer.deserialzeCustomer();
System.out.println(customer);
}
public Customer deserialzeCustomer(){
Customer customer;
try{
FileInputStream fin = new FileInputStream("c:\\customer.txt");
ObjectInputStream ois = new ObjectInputStream(fin);
customer = (Customer) ois.readObject();
ois.close();
return customer;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
另请参阅下面给出的 Customer 类:
package com.shineed.io;
import java.io.Serializable;
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String adress;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
}