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();
- }
- }
- }
以上就是对Java Socket传输数据的详细介绍,希望大家有所收获。
【编辑推荐】