java.io.EOFException
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 + "]";
}
}
为什么会报异常啊
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 + "]";
}
}
为什么会报异常啊
...全文
相关推荐
java.io.EOFException(解决方案).md
Java中的`java.io.EOFException`异常是I/O操作中一个常见的异常,它在输入流到达文件末尾时被抛出。
EOFException解决办法.md
在这里,如果捕获的异常是EOFException,程序会输出一条特定的错误消息,表明已经到达了文件末尾。如果是其他类型的IO异常,则输出相应的错误信息。
EOFException如何解决.md
当程序在读取文件时已经到达文件末尾,但程序逻辑没有正确处理这种情况时,EOFException就发生了。这种情况在使用BufferedReader、Scanner等IO类时尤其常见。
io流各种组合使用与总结
### Java IO流的各种组合使用与总结在Java中,输入/输出(IO)流是进行数据读取和写入操作的基础。本文将详细介绍Java中IO流的多种组合使用方法,并结合示例代码来阐述每种配置的具体应用。
socket编程
socket编程,信息的互动性java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Objec


Java SE
