Java Tutorial/File/Externalizable
Версия от 17:44, 31 мая 2010; (обсуждение)
implements Externalizable
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 MainClass {
public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("Constructing objects:");
MyBean myBean = new MyBean("A String ", 47);
System.out.println(myBean);
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("MyBean.out"));
System.out.println("Saving object:");
o.writeObject(myBean);
o.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("MyBean.out"));
System.out.println("Recovering:");
myBean = (MyBean) in.readObject();
System.out.println(myBean);
}
}
class MyBean implements Externalizable {
private int i;
private String s; // No initialization
public MyBean() {
System.out.println("MyBean Constructor");
// s, i not initialized
}
public MyBean(String x, int a) {
System.out.println("MyBean(String x, int a)");
s = x;
i = a;
// s & i initialized only in nondefault constructor.
}
public String toString() {
return s + i;
}
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("MyBean.writeExternal");
out.writeObject(s);
out.writeInt(i);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("MyBean.readExternal");
s = (String) in.readObject();
i = in.readInt();
}
}
/**/
Constructing objects: MyBean(String x, int a) A String 47 Saving object: MyBean.writeExternal Recovering: MyBean Constructor MyBean.readExternal A String 47
Reconstructing an externalizable object
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 MainClass {
public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("Constructing objects:");
MyBean myBean = new MyBean("A String ", 47);
System.out.println(myBean);
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("MyBean.out"));
System.out.println("Saving object:");
o.writeObject(myBean);
o.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("MyBean.out"));
System.out.println("Recovering:");
myBean = (MyBean) in.readObject();
System.out.println(myBean);
}
}
class MyBean implements Externalizable {
private int i;
private String s; // No initialization
public MyBean() {
System.out.println("MyBean Constructor");
// s, i not initialized
}
public MyBean(String x, int a) {
System.out.println("MyBean(String x, int a)");
s = x;
i = a;
// s & i initialized only in nondefault constructor.
}
public String toString() {
return s + i;
}
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("MyBean.writeExternal");
out.writeObject(s);
out.writeInt(i);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("MyBean.readExternal");
s = (String) in.readObject();
i = in.readInt();
}
}
/**/
Constructing objects: MyBean(String x, int a) A String 47 Saving object: MyBean.writeExternal Recovering: MyBean Constructor MyBean.readExternal A String 47
Simple use of Externalizable
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 A implements Externalizable {
public A() {
System.out.println("A Constructor");
}
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("A.writeExternal");
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("A.readExternal");
}
}
class B implements Externalizable {
B() {
System.out.println("B Constructor");
}
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("B.writeExternal");
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("B.readExternal");
}
}
public class MainClass {
public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("Constructing objects:");
A b1 = new A();
B b2 = new B();
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("File.out"));
System.out.println("Saving objects:");
o.writeObject(b1);
o.writeObject(b2);
o.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("File.out"));
System.out.println("Recovering b1:");
b1 = (A) in.readObject();
}
}
/*
*/
Constructing objects: A Constructor B Constructor Saving objects: A.writeExternal B.writeExternal Recovering b1: A Constructor A.readExternal