JAVA Socket传输数据和对象(2)

我们将这个对象串行化至文件系统,然后将之还原,这个过程其实类似于一个“压扁”和“充气”的过程,请注意,我们的Person类中包含一个嵌入对象,并且birthday变化,将之设置为transient限定符,这表示我们放弃了birthday的串行化;
Java代码
package stream.demo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;
public class Persistence {
public static void main(String[] args) {
Persistence.savePerson();
Persistence.getPerson();
}
public static void getPerson() {
try {
InputStream in = new FileInputStream("c:\\person.dat");
ObjectInputStream dataInput = new ObjectInputStream(in);
Person p = (Person) dataInput.readObject();
System.out.println(p.getName());
System.out.println(p.getTall());
System.out.println(p.getBirthday());
System.out.println(p.getAddress().getCity());
System.out.println(p.getAddress().getStreet());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void savePerson() {
Person p = new Person();
p.setName("corey");
p.setTall(171);
p.setBirthday(new Date());
p.setAddress(new Address("yiyang", "ziyang"));
OutputStream out = new ByteArrayOutputStream();
try {
OutputStream fileOut = new FileOutputStream(new File(
"c:\\person.dat"));
ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);
dataOut.writeObject(p);
dataOut.close();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package stream.demo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;
public class Persistence {
public static void main(String[] args) {
Persistence.savePerson();
Persistence.getPerson();
}
public static void getPerson() {
try {
InputStream in = new FileInputStream("c:\\person.dat");
ObjectInputStream dataInput = new ObjectInputStream(in);
Person p = (Person) dataInput.readObject();
System.out.println(p.getName());
System.out.println(p.getTall());
System.out.println(p.getBirthday());
System.out.println(p.getAddress().getCity());
System.out.println(p.getAddress().getStreet());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void savePerson() {
Person p = new Person();
p.setName("corey");
p.setTall(171);
p.setBirthday(new Date());
p.setAddress(new Address("yiyang", "ziyang"));
OutputStream out = new ByteArrayOutputStream();
try {
OutputStream fileOut = new FileOutputStream(new File(
"c:\\person.dat"));
ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);
dataOut.writeObject(p);
dataOut.close();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpdpy.html