Java Socket传输数据的文件系统介绍
Java Socket传输数据在进行的时候有很多的事情需要我们不断的进行有关代码的学习。只有不断的学习才能掌握相关的问题。下面我们就详细的看看如何才能更好的使用这些技术。
我们将这个对象串行化至文件系统,然后将之还原,Java Socket传输数据在这个过程其实类似于一个“压扁”和“充气”的过程,请注意,我们的Person类中包含一个嵌入对象,并且birthday变化,将之设置为transient限定符,这表示我们放弃了birthday的串行化;
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 }
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 }
以上是 Java Socket传输数据的文件系统介绍 的全部内容, 来源链接: utcz.com/z/390656.html