Java 시리얼화, 실전 코드 역시리얼화(에세이)

3975 단어 Java 고급
인터페이스 클래스를 시리얼화하는 솔리드 클래스를 만들려면 다음과 같이 하십시오.
package com.kgc7;

import java.io.Serializable;
//    Student ,    (implements Serializable)
public class Student implements Serializable{
    String name;
    String gender;
    int age;

    public Student(String name, String gender, int age) {
	this.name = name;
	this.gender = gender;
	this.age = age;
    }
}

단일 솔리드 시리얼화 테스트:
package com.kgc7;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
//            "stu.txt",         FileOutputStream,     ObjectOutputStream。
public class Test {
    public static void main(String[] args) {
	FileOutputStream fos = null;
	ObjectOutputStream oos = null;
	try {
	    fos = new FileOutputStream("stu.txt");
	    oos = new ObjectOutputStream(fos);
	    Student stu = new Student("  ", " ", 26);
	    oos.writeObject(stu);
	    oos.flush();   //  
	    oos.close();   //   
	    fos.close();
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}

다중 솔리드 시리얼화 테스트:
package com.kgc7;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test1 {
    /**
     *          
     * 
     * @param args
     */
    public static void main(String[] args) {
	// TODO Auto-generated method stub
	FileOutputStream fos = null;
	ObjectOutputStream oos = null;
	try {
	    fos = new FileOutputStream("stu.txt");
	    oos = new ObjectOutputStream(fos);
	    Student stu = new Student("  ", " ", 26);
	    Student stu1 = new Student("  ", " ", 20);
	    //           
	    ArrayList stuList = new ArrayList();
	    //        
	    stuList.add(stu);
	    stuList.add(stu1);
	    //      
	    oos.writeObject(stuList);
	    //   
	    oos.flush();
	    oos.close();
	    fos.close();
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}

텍스트 파일 컨텐트 역정렬 테스트:
package com.kgc7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *           
 * 
 * @author Administrator
 *
 */
public class Test2 {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
	// TODO Auto-generated method stub
	//                  FileInputStream;     ObjectInputStream。
	FileInputStream fis = null;
	ObjectInputStream ois = null;

	try {
	    fis = new FileInputStream("stu1.txt");
	    ois = new ObjectInputStream(fis);
	    //          list  ,ArrayList    ,       。
	    ArrayList list = (ArrayList) ois.readObject();
	    //                  (   ),            。
	    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
		Student student = (Student) iterator.next();
		System.out.println(student.name + "\t" + student.gender + "\t" + student.age);
	    }
	    //    
	    ois.close();
	    fis.close();
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}

결과 출력("stu1.txt"텍스트 파일 내용):
     26

     20

좋은 웹페이지 즐겨찾기