Java Socket传输数据在进行的时候有很多的事情需要我们不断的进行有关代码的学习。只有不断的学习才能掌握相关的问题。下面我们就详细的看看如何才能更好的使用这些技术。
我们将这个对象串行化至文件系统,然后将之还原,Java Socket传输数据在这个过程其实类似于一个“压扁”和“充气”的过程,请注意,我们的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(); } } }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
以上就是对Java Socket传输数据的详细介绍,希望大家有所收获。
【编辑推荐】