Serialization collection

com.itcast.demo06.ObjectStream Package ; 

Import the java.io. * ;
Import of java.util.ArrayList ;

/ **
* @author newcityman
* @date 2019/7/28 - 23:15
* Exercise: sequence sets with
* Step :
* 1, define a storage Person object ArrayList set
* 2, to a collection stored in the person object
* 3, to create a serialized stream ObjectOutputStream, the collection serializing
* 4, a method using an ObjectOutputStream object the writeObject, for collection serialization
* 5, creating a deserialized ObjectInputStream stream
*. 6, a method using ObjectInputStream object readObject read the saved file collection
* 7, converted into a set of type Object type ArrayList
* 8, binding to traverse ArrayList
* 9, free up resources
* /
public class Demo03Test {
public static voidmain (String [] args) throws IOException , a ClassNotFoundException {
//. 1, a memory definition of the ArrayList Person object set
the ArrayList <Person> = plist new new the ArrayList <> () ;
// 2, to a collection stored in the person object
pList.add ( new new the Person ( "Zhangshan Feng" , 70)) ;
pList.add ( new new the Person ( "Zhao Yiyong" , 20)) ;
pList.add ( new new the Person ( "Min" , 30)) ;
// 3. create a serialized stream ObjectOutputStream, the sequence of collection of
the ObjectOutputStream OOS = new new the ObjectOutputStream ( new new a FileOutputStream ( "day18_IOAndProperties \\ person.txt")) ;
// 4, using the method of the writeObject ObjectOutputStream object, the sequence of collection of
oos.writeObject (plist) ;
//. 5, creating a flow deserialized the ObjectInputStream
the ObjectInputStream OIS = new new the ObjectInputStream ( new new the FileInputStream ( "day18_IOAndProperties \\ Person. TXT ")) ;
save // 6, method ObjectInputStream readObject object file is read and set
Object ois.readObject O = () ;
//. 7, to convert a collection of type Object to ArrayList of
ArrayList <Person> list = (ArrayList <the Person>) O ;
//. 8, traversing ArrayList binding for (the Person Person: List) { . the System Out.println (Person) ; } ois.close () ; oos.close () ; } }







Guess you like

Origin www.cnblogs.com/newcityboy/p/11261547.html