Stream Package; Import a java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; Import java.io.ObjectInputStream; Import java.io.ObjectOutputStream; Import the java.io. the Serializable; Import javax.security.auth.login.AccountException; Import org.junit.jupiter.api.Test; / * * target stream * read and stored with the basic data types or data objects Liu process. The strengths is that it is possible to java objects to stream * * * / public class ObjectInputStreamTeat { / * * serialization process: Save the java object in memory to disk or outgoing network transmission * use the ObjectOutputStream * * / @Test public void testObjectOutputStream() { ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("Hello3.txt")); oos.writeObject(new String("我是中国人")); oos.flush(); oos.writeObject(new Person("zsben",1,new Account(2000))); oos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(oos!=null) oos.close(); } catch (Exception e) { e.printStackTrace(); } } } /* * 反序列化:用ObjectInputStream实现 * */ @Test public void testObjectInputStream() { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("Hello3.txt")); Object object = ois.readObject(); String string = (String)object; System.out.println(string); Object object2 = ois.readObject(); Person person = (Person)object2; System.out.println(person); } the catch (Exception e) { e.printStackTrace (); } the finally { the try { IF (OIS =! null ) ois.close (); } the catch (Exception E) { e.printStackTrace (); } } } } / * * class to implement 1.Person serializable must implement the interface serializable serializable * 2. also need to add a final class attribute UID * 3. in addition to the current Person class needs to implement serializable, but also to ensure that all internal attributes are serializable * (default the basic data type is serializable) * serialized object stream can not be modified static and transient member * * / class the Person the implements the serializable { public static final long serivalVersionUID = 5432146546351568416L; private String name; private static int age; private Account account; @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", account=" + account + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person(String name, int age, Account account) { super(); this.name = name; this.age = age; this.account = account; } public Person() { super(); } } class Account implements Serializable{ private double balance; static final public long serivalVersionUID = 54685237864535874L; @Override public String toString() { return "Account [balance=" + balance + "]"; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public Account(double balance) { super(); this.balance = balance; } public Account() { super(); } }
java-based - the stream of objects, serialization mechanism Serializable
Guess you like
Origin www.cnblogs.com/zsben991126/p/12160999.html
Ranking