Java 流已关闭:运行时错误消息
//在运行Java 流已关闭:运行时错误消息,java,Java,//在运行Aamirclass时,我收到了这个错误 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 ja
Aamir
class时,我收到了这个错误
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);
}
}您收到一个错误,因为您试图在关闭流后写入流
您不需要在writeExternal中刷新或关闭流。您将收到一个错误,因为您试图在关闭流后写入流 您不需要在writeExternal中刷新或关闭流。您可以
.close()
在.writeExternal()中关闭流。
;你不应该
在Externalizable
的合同中从来没有说过这种方法应该关闭流;如果类继承您并调用super.writeExternal(),该怎么办?您.close()
中的流.writeExternal()
;你不应该
在
Externalizable
的合同中从来没有说过这种方法应该关闭流;如果一个类继承您并调用super.writeExternal(),该怎么办?您尝试关闭ObjectOutputStream两次
在:
FileOutputStream fout=newfileoutputstream(“dev.txt”);
ObjectOutputStream out=新的ObjectOutputStream(fout);
p1.外部写入(输出);
p2.书面外部(外部);
out.flush();
out.close() 您尝试关闭ObjectOutputStream两次
在:
FileOutputStream fout=newfileoutputstream(“dev.txt”);
ObjectOutputStream out=新的ObjectOutputStream(fout);
p1.外部写入(输出);
p2.书面外部(外部);
out.flush();
out.close() 获取运行时异常的原因是,您关闭了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);
}
}
获取运行时异常的原因是,您关闭了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);
}
}
嗯,什么错误?什么时候抛出错误?(行号)嗯,什么错误?什么时候抛出错误?(行号)当我注释/*out.flush()时;out.close()*/它仍然显示相同的错误;out.close()*/它仍然显示相同的错误。
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);
}
}