Java Socket傳輸數(shù)據(jù)的文件系統(tǒng)介紹
Java Socket傳輸數(shù)據(jù)在進(jìn)行的時(shí)候有很多的事情需要我們不斷的進(jìn)行有關(guān)代碼的學(xué)習(xí)。只有不斷的學(xué)習(xí)才能掌握相關(guān)的問(wèn)題。下面我們就詳細(xì)的看看如何才能更好的使用這些技術(shù)。
我們將這個(gè)對(duì)象串行化至文件系統(tǒng),然后將之還原,Java Socket傳輸數(shù)據(jù)在這個(gè)過(guò)程其實(shí)類似于一個(gè)“壓扁”和“充氣”的過(guò)程,請(qǐng)注意,我們的Person類中包含一個(gè)嵌入對(duì)象,并且birthday變化,將之設(shè)置為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();
 - }
 - }
 - }
 
以上就是對(duì)Java Socket傳輸數(shù)據(jù)的詳細(xì)介紹,希望大家有所收獲。
【編輯推薦】















 
 
 


 
 
 
 