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