//在运行Aamir
课程时,我收到此错误。
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public class Aamir {
public static void main(String[] args) throws Exception {
Externalize();
deExternalize();
}
public static void Externalize() throws IOException {
Person p1 = new Person();
p1.name = "aamir";
p1.age = 22;
p1.weight = 60.80;
Person p2 = new Person();
p2.name = "zahid";
p2.age = 26;
p2.weight = 64.88;
FileOutputStream fout = new FileOutputStream("dev.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
p1.writeExternal(out);
p2.writeExternal(out);
out.flush();
out.close();
}
public static void deExternalize() throws IOException, ClassNotFoundException {
FileInputStream fin = new FileInputStream("dev.ser");
ObjectInputStream in = new ObjectInputStream(fin);
Person p3 = new Person();
p3.readExternal(in);
Person p4 = new Person();
p4.readExternal(in);
}
}
class Person implements Externalizable {
String name;
int age;
double weight;
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(age);
out.writeDouble(weight);
out.flush();
out.close();
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = in.readUTF();
age = in.readInt();
weight = in.readDouble();
System.out.println(name);
System.out.println(age);
System.out.println(weight);
}
}
答案 0 :(得分:1)
您收到错误是因为您在关闭它后尝试写入流。
您不需要在writeExternal中刷新或关闭流。
答案 1 :(得分:1)
您.close()
中的.writeExternal()
个流;你不应该。
Externalizable
的合同中从未说过这种方法应该关闭流;怎么一个类继承你并调用super.writeExternal()?
答案 2 :(得分:1)
您尝试关闭ObjectOutputStream两次。
in:
FileOutputStream fout = new FileOutputStream("dev.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
p1.writeExternal(out);
p2.writeExternal(out);
out.flush();
out.close(); <-----
在这里:
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(age);
out.writeDouble(weight);
out.flush();
out.close(); <-----
你不应该在writeExternal中关闭它。
答案 3 :(得分:1)
您获得运行时异常的原因是因为您关闭了ObjectOutputStream资源,但是再次通过该“out”引用尝试编写p2数据。所以这是正确的代码
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
class Aamir {
public static void main(String[] args) throws Exception {
Externalize();
deExternalize();
}
public static void Externalize() throws IOException {
Person p1 = new Person();
p1.name = "aamir";
p1.age = 22;
p1.weight = 60.80;
Person p2 = new Person();
p2.name = "zahid";
p2.age = 26;
p2.weight = 64.88;
FileOutputStream fout = new FileOutputStream("dev.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
p1.writeExternal(out);
p2.writeExternal(out);
out.flush();
out.close();
}
public static void deExternalize() throws IOException, ClassNotFoundException {
FileInputStream fin = new FileInputStream("dev.ser");
ObjectInputStream in = new ObjectInputStream(fin);
Person p3 = new Person();
p3.readExternal(in);
Person p4 = new Person();
p4.readExternal(in);
}
}
class Person implements Externalizable {
String name;
int age;
double weight;
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(age);
out.writeDouble(weight);
// out.flush();
//out.close();
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = in.readUTF();
age = in.readInt();
weight = in.readDouble();
System.out.println(name);
System.out.println(age);
System.out.println(weight);
}
}