把user1这个类实现Serializable接口,如果你的这个类是User1,则如下所示:
public class User1 implements Serializable{
File file = new File("c:\\db.bak");
if(!file.exists())
try {
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("db.bak"));
oos.writeObject(user1);
System.out.println("用户数据正在写入");
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} |