欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
53.package stream.demo;
54.import java.io.ByteArrayInputStream;
55.import java.io.ByteArrayOutputStream;
56.import java.io.File;
57.import java.io.FileInputStream;
58.import java.io.FileOutputStream;
59.import java.io.IOException;
60.import java.io.InputStream;
61.import java.io.ObjectInputStream;
62.import java.io.ObjectOutputStream;
63.import java.io.OutputStream;
64.import java.util.Date;
65.public class Persistence {
66.public static void main(String[] args) {
67.Persistence.savePerson();
68.Persistence.getPerson();
69.}
70.public static void getPerson() {
71.try {
72.InputStream in = new FileInputStream(“c:\\person.dat”);
73.ObjectInputStream dataInput = new ObjectInputStream(in);
74.Person p = (Person) dataInput.readObject();
75.System.out.println(p.getName());
76.System.out.println(p.getTall());
77.System.out.println(p.getBirthday());
78.System.out.println(p.getAddress().getCity());
79.System.out.println(p.getAddress().getStreet());
80.} catch (Exception e) {
81.// TODO Auto-generated catch block
82.e.printStackTrace();
83.}
84.}
85.public static void savePerson() {
86.Person p = new Person();
87.p.setName(“corey”);
88.p.setTall(171);
89.p.setBirthday(new Date());
90.p.setAddress(new Address(“yiyang”, “ziyang”));
91.OutputStream out = new ByteArrayOutputStream();
92.try {
93.OutputStream fileOut = new FileOutputStream(new File(
94.”c:\\person.dat”));
95.ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);
96.dataOut.writeObject(p);
97.dataOut.close();
98.fileOut.close();
99.} catch (IOException e) {
100.// TODO Auto-generated catch block
101.e.printStackTrace();
102.}
103.}
104.}
以上就是对Java Socket传输数据的详细介绍,希望大家有所收获。
[1][2]
获致幸福的不二法门是珍视你所拥有的遗忘你所没有的