JavaSocket传输数据在进行的时候有很多的事情需要我们不断的进行有关代码的学习.只有不断的学习才能掌握相关的问题.下面我们就详细的看看如何才能更好的使用这些技术.
我们将这个对象串行化至文件系统,然后将之还原,Java <http://developer.51cto.com/art/200512/15883.htm> Socket传输数据在这个过程其实类似于一个“压扁”和“充气”的过程,请注意,我们的Person类中包含一个嵌入对象,并且birthday变化,将之设置为transient限定符,这表示我们放弃了birthday的串行化;
Java代码
1.package stream.demo;
2.import java.io.ByteArrayInputStream;
3.import java.io.ByteArrayOutputStream;
4.import java.io.File;
5.import java.io.FileInputStream;
6.import java.io.FileOutputStream;
7.import java.io.IOException;
8.import java.io.InputStream;
9.import java.io.ObjectInputStream;
10.import java.io.ObjectOutputStream;
11.import java.io.OutputStream;
12.import java.util.Date;
13.public class Persistence {
14.public static void main(String[] args) {
15.Persistence.savePerson();
16.Persistence.getPerson();
17.}
18.public static void getPerson() {
19.try {
20.InputStream in = new FileInputStream("c:\person.dat");
21.ObjectInputStream dataInput = new ObjectInputStream(in);
22.Person p = (Person) dataInput.readObject();
23.System.out.println(p.getName());
24.System.out.println(p.getTall());
25.System.out.println(p.getBirthday());
26.System.out.println(p.getAddress().getCity());
27.System.out.println(p.getAddress().getStreet());
28.} catch (Exception e) {
29.// TODO Auto-generated catch block
30.e.printStackTrace();
31.}
32.}
33.public static void savePerson() {
34.Person p = new Person();
35.p.setName("corey");
36.p.setTall(171);
37.p.setBirthday(new Date());
38.p.setAddress(new Address("yiyang", "ziyang"));
39.OutputStream out = new ByteArrayOutputStream();
40.try {
41.OutputStream fileOut = new FileOutputStream(new File(
42."c:\person.dat"));
43.ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);
44.dataOut.writeObject(p);
45.dataOut.close();
46.fileOut.close();
47.} catch (IOException e) {
48.// TODO Auto-generated catch block
49.e.printStackTrace();
50.}
51.}
52.}
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传输数据的详细介绍,希望大家有所收获.