java.io.EOFException

zhx86890057 zhx86890057 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Test {

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
File file = new File("src/work4/member.txt");
write(file);
read(file);
}

private static void read(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Object obj = null;
while((obj=ois.readObject())!=null){
System.out.println(obj);
}
ois.close();
}

private static void write(File file) throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new Member("aaa","123","men","hangzhou"));
oos.writeObject(new Member("ccc","143","women","shanghai"));
oos.writeObject(new Member("bbb","444","men","beijing"));
oos.flush();
oos.close();
}

}
class Member implements Serializable{
String user;
transient String password;
String sex;
String address;
public Member(String user, String password, String sex, String address) {
super();
this.user = user;
this.password = password;
this.sex = sex;
this.address = address;
}
@Override
public String toString() {
return "Member [user=" + user + ", sex=" + sex + ", address=" + address + "]";
}
}
为什么会报异常啊
...全文
评论 2
收藏 已收藏
分享
返回顶部
zhx86890057
Java SE 62,620 成员 307,251 内容