您好,登錄后才能下訂單哦!
Java 中怎么利用Socket傳輸數(shù)據(jù),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
我們將這個(gè)對(duì)象串行化至文件系統(tǒng),然后將之還原,Java Socket傳輸數(shù)據(jù)在這個(gè)過(guò)程其實(shí)類(lèi)似于一個(gè)“壓扁”和“充氣”的過(guò)程,請(qǐng)注意,我們的Person類(lèi)中包含一個(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(); } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。