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(); } } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。