Object Serialization and Persistence 对象序列化和持久化 Institute of Computer Software 2022-2-27 Nanjing University
对象序列化和持久化 Object Serialization and Persistence 2022-2-27 Institute of Computer Software Nanjing University 1
&雪扇 摘要 UNIVE 2 口对象序列化 口对象持久化 ▣Language level ▣Databases Hibernate Institute of Computer Software 2022-2-27 Nanjing University
摘要 对象序列化 对象持久化 Language level Databases Hibernate 2022-2-27 Institute of Computer Software Nanjing University 2
&雪扇 摘要 UNIVE 口对象序列化 口对象持久化 ▣Language level ▣Databases Hibernate Institute of Computer Software 2022-2-27 Nanjing University
摘要 对象序列化 对象持久化 Language level Databases Hibernate 2022-2-27 Institute of Computer Software Nanjing University 3
&雪扇 摘要 UNIVE 4 口对象序列化 口对象持久化 ▣Language level ▣Databases Hibernate Institute of Computer Software 2022-2-27 Nanjing University
摘要 对象序列化 对象持久化 Language level Databases Hibernate 2022-2-27 Institute of Computer Software Nanjing University 4
&扇 Object Serialization o Why What 0 How Institute of Computer Software 2022-2-27 Nanjing University
Object Serialization Why What How 2022-2-27 Institute of Computer Software Nanjing University 5
&扇 Java Object Serialization --Why 1002 UNIVE 6 ▣ Serialization is used for lightweight persistence and for communication via sockets or Remote Method Invocation(RMI). Institute of Computer Software 2022-2-27 Nanjing University
Java Object Serialization -- Why Serialization is used for lightweight persistence and for communication via sockets or Remote Method Invocation (RMI). 2022-2-27 Institute of Computer Software Nanjing University 6
Java Object Serialization --Example 102 public class Client{ public static void main(String args[]){ try {//Create a socket Socket soc new Socket(InetAddress.getLocalHost(),8020); OutputStream o soc.getOutputStream(); ObjectOutput s new ObjectOutputStream(o);s.writeObject("Today's date"); s.writeObject(new Date()) s.flush(); s.close(); catch(Exception e){ System.out.println(e.getMessage()); System.out.println("Error during serialization"); System.exit(1); Institute of Computer Software 2022-2-27 Nanjing University
Java Object Serialization -- Example public class Client { public static void main(String args[]) { try { // Create a socket Socket soc = new Socket(InetAddress.getLocalHost(), 8020); OutputStream o = soc.getOutputStream(); ObjectOutput s = new ObjectOutputStream(o); s.writeObject("Today ' s date "); s.writeObject(new Date()); s.flush(); s.close(); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("Error during serialization "); System.exit(1); } } } 2022-2-27 Institute of Computer Software Nanjing University 7
&扇 Java Object Serialization --Example public class Server public static void main(String args){ ServerSocket ser null; Socket soc null; String str null; Date d null; try{ ser new ServerSocket(8020); soc ser.accept(); InputStream o soc.getlnputStream(); ObjectInput s=new ObjectInputStream(o); str =(String)s.readObject(); d =(Date)s.readObject(); s.close(); System.out.println(str);System.out.printIn(d); catch(Exception e){ System.out.printIn(e.getMessage()); System.out.println("Error during serialization"); System.exit(1); } Institute of Computer Software 2022-2-27 Nanjing University
Java Object Serialization -- Example public class Server { public static void main(String args[]) { ServerSocket ser = null; Socket soc = null; String str = null; Date d = null; try { ser = new ServerSocket(8020); soc = ser.accept(); InputStream o = soc.getInputStream(); ObjectInput s = new ObjectInputStream(o); str = (String) s.readObject(); d = (Date) s.readObject(); s.close(); System.out.println(str); System.out.println(d); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("Error during serialization"); System.exit(1); } } } 2022-2-27 Institute of Computer Software Nanjing University 8
&扇 Java Object Serialization -Example 1902 9 Writing to an object stream /Serialize today's date to a file. FileOutputStream f new FileOutputStream("tmp"); ObjectOutput s new ObjectOutputStream(f); s.writeObject("Today"); s.writeObject(new Date()); s.flush(); Institute of Computer Software 2022-2-27 Nanjing University
Java Object Serialization -- Example Writing to an object stream 2022-2-27 Institute of Computer Software Nanjing University 9 // Serialize today's date to a file. FileOutputStream f = new FileOutputStream("tmp"); ObjectOutput s = new ObjectOutputStream(f); s.writeObject("Today"); s.writeObject(new Date()); s.flush();
Java Object Serialization -Example 1902 10 Reading from an object stream /Deserialize a string and date from a file. FilelnputStream in new FilelnputStream("tmp"); ObjectInputStream s new ObjectInputStream(in); String today =(String)s.readObject(); Date date =(Date)s.readObject(); Institute of Computer Software 2022-2-27 Nanjing University
Java Object Serialization -- Example Reading from an object stream 2022-2-27 Institute of Computer Software Nanjing University 10 // Deserialize a string and date from a file. FileInputStream in = new FileInputStream("tmp"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject();