对象实现序列化接口
public class User implements Serializable {
}
写入文件
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("/home/user.txt"));
os.writeObject(user);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
读取文件
try {
ObjectInputStreamos = new ObjectInputStream(new FileInputStream("/home/user.txt"));
User user = (User ) is.readObject();
} catch (Exception e) {
e.printStackTrace();
}