java.io.EOFException

zhx86890057 2016-11-21 09:41:53
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 + "]";
}
}
为什么会报异常啊
...全文
187 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
什么异常
  • 打赏
  • 举报
回复
关联的文件名对嘛? src\\work4\\member.txt //我怎么记得是这样的啊 ; 你这个写应该没问题,异常是读的时候... 我也不知道 ; 但是我建议你这个对象输入流,可以试试建立一个集合ArrayList<Member>来装对象,然后用对象操作流写入 , 对象操作流读取的时候 , 也用集合接收 , 在遍历集合 ;

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧