熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java Socket傳輸數據的文件系統介紹

2022-06-13   來源: Java核心技術 

  Java Socket傳輸數據在進行的時候有很多的事情需要我們不斷的進行有關代碼的學習只有不斷的學習才能掌握相關的問題下面我們就詳細的看看如何才能更好的使用這些技術

  我們將這個對象串行化至文件系統然後將之還原Java <> Socket傳輸數據在這個過程其實類似於一個壓扁充氣的過程請注意我們的Person類中包含一個嵌入對象並且birthday變化將之設置為transient限定符這表示我們放棄了birthday的串行化

  Java代碼

  package streamdemo;

  import javaioByteArrayInputStream;

  import javaioByteArrayOutputStream;

  import javaioFile;

  import javaioFileInputStream;

  import javaioFileOutputStream;

  import javaioIOException;

  import javaioInputStream;

  import javaioObjectInputStream;

  import javaioObjectOutputStream;

  import javaioOutputStream;

  import javautilDate;

  public class Persistence {

  public static void main(String[] args) {

  PersistencesavePerson();

  PersistencegetPerson();

  }

  public static void getPerson() {

  try {

  InputStream in = new FileInputStream(c:\\persondat);

  ObjectInputStream dataInput = new ObjectInputStream(in);

  Person p = (Person) dataInputreadObject();

  Systemoutprintln(pgetName());

  Systemoutprintln(pgetTall());

  Systemoutprintln(pgetBirthday());

  Systemoutprintln(pgetAddress()getCity());

  Systemoutprintln(pgetAddress()getStreet());

  } catch (Exception e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  public static void savePerson() {

  Person p = new Person();

  psetName(corey);

  psetTall();

  psetBirthday(new Date());

  psetAddress(new Address(yiyang ziyang));

  OutputStream out = new ByteArrayOutputStream();

  try {

  OutputStream fileOut = new FileOutputStream(new File(

  c:\\persondat));

  ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);

  dataOutwriteObject(p);

  dataOutclose();

  fileOutclose();

  } catch (IOException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  }

  package streamdemo;

  import javaioByteArrayInputStream;

  import javaioByteArrayOutputStream;

  import javaioFile;

  import javaioFileInputStream;

  import javaioFileOutputStream;

  import javaioIOException;

  import javaioInputStream;

  import javaioObjectInputStream;

  import javaioObjectOutputStream;

  import javaioOutputStream;

  import javautilDate;

  public class Persistence {

  public static void main(String[] args) {

  PersistencesavePerson();

  PersistencegetPerson();

  }

  public static void getPerson() {

  try {

  InputStream in = new FileInputStream(c:\\persondat);

  ObjectInputStream dataInput = new ObjectInputStream(in);

  Person p = (Person) dataInputreadObject();

  Systemoutprintln(pgetName());

  Systemoutprintln(pgetTall());

  Systemoutprintln(pgetBirthday());

  Systemoutprintln(pgetAddress()getCity());

  Systemoutprintln(pgetAddress()getStreet());

  } catch (Exception e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  public static void savePerson() {

  Person p = new Person();

  psetName(corey);

  psetTall();

  psetBirthday(new Date());

  psetAddress(new Address(yiyang ziyang));

  OutputStream out = new ByteArrayOutputStream();

  try {

  OutputStream fileOut = new FileOutputStream(new File(

  c:\\persondat));

  ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);

  dataOutwriteObject(p);

  dataOutclose();

  fileOutclose();

  } catch (IOException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  }

  以上就是對Java Socket傳輸數據的詳細介紹希望大家有所收獲


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27200.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.