Reputation: 2424
// On running Aamir
class I am getting this error.
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);
}
}
Upvotes: 0
Views: 394
Reputation: 2515
The reason you were getting runtime exception is because you we closing ObjectOutputStream resource but again through that "out" reference you are trying to write p2 data. So here is the correct code
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);
}
}
Upvotes: 1
Reputation: 5123
You try and close the ObjectOutputStream twice.
in:
FileOutputStream fout = new FileOutputStream("dev.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
p1.writeExternal(out);
p2.writeExternal(out);
out.flush();
out.close(); <-----
and here:
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(age);
out.writeDouble(weight);
out.flush();
out.close(); <-----
You should not close it in writeExternal.
Upvotes: 1
Reputation: 121692
You .close()
the stream in .writeExternal()
; you should not.
It is never said in the contract of Externalizable
that this method should close the stream; what if a class inherits you and calls super.writeExternal()?
Upvotes: 1
Reputation: 533432
You are getting an error because you are trying to write to the stream after you closed it.
You don't need to flush or close the stream in writeExternal.
Upvotes: 1