對象序列化-讀取/存入文件ObjectOutputStream/ObjectInputStream

 ----------------------    java

                     
  1. package com.io.file;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.ObjectInputStream;  
  8. import java.io.ObjectOutputStream;  
  9. import java.io.Serializable;  
  10.   
  11. /* 
  12.  * 對對象進行操做,把對象數據存入到文件中,再從文件中讀取 
  13.  */  
  14. public class Object_in_out {  
  15.     public static void main(String[] args) {  
  16.         try {  
  17. //          write();   
  18.                 read();  
  19.         } catch (ClassNotFoundException e) {  
  20.             // TODO Auto-generated catch block   
  21.             e.printStackTrace();  
  22.         } catch (FileNotFoundException e) {  
  23.             e.printStackTrace();  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.     public static void read() throws FileNotFoundException, IOException, ClassNotFoundException{  
  29.         ObjectInputStream ois=new ObjectInputStream(new FileInputStream("g:/java/object.txt"));  
  30.         Person p=(Person)ois.readObject();  
  31.         System.out.println(p.getName()+",,,,,,,,"+p.getAge());  
  32.         ois.close();  
  33.     }  
  34.     public static void write() throws FileNotFoundException, IOException{  
  35.         ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("g:/java/object.txt"));  
  36.         oos.writeObject(new Person("aaa",33));//把對象寫入到文件中   
  37.         oos.close();  
  38.     }  
  39. }  
  40. class Person implements Serializable{//注意這個對象類要實現Serializable序列化接口   
  41.     //此接口會爲該對象生成一個根據該對象的序列化id,若是本對象改變了,調用本對象的類要從新編譯才行   
  42.     //或者直接指定本對象的序列化id   
  43.     public static final long serialVersionUID = 42L;//爲本對象生成一個固定的系列化id,這樣本類改變的話,調用類就不用從新編譯了   
  44.   
  45.     private String name;  
  46.     public static String country="";//注意這裏,靜態不能序列化,由於序列化的在對內存中,而靜態在方法區中   
  47.     private int age;  
  48.     //若是不想被序列化,但又不定義靜態,能夠用transient關鍵字修飾,保證被修飾的能夠在對內存中存在,但不能存放到文本文件中   
  49.     //如這樣定義就能夠了:private transient int ages;   
  50.     public Person(String name,int age){  
  51.         this.name=name;  
  52.         this.age=age;  
  53.     }  
  54.     public String getName() {  
  55.         return name;  
  56.     }  
  57.     public void setName(String name) {  
  58.         this.name = name;  
  59.     }  
  60.     public int getAge() {  
  61.         return age;  
  62.     }  
  63.     public void setAge(int age) {  
  64.         this.age = age;  
  65.     }  
  66.       
  67. }  
package com.io.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/*
 * 對對象進行操做,把對象數據存入到文件中,再從文件中讀取
 */
public class Object_in_out {
	public static void main(String[] args) {
		try {
//			write();
				read();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void read() throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream("g:/java/object.txt"));
		Person p=(Person)ois.readObject();
		System.out.println(p.getName()+",,,,,,,,"+p.getAge());
		ois.close();
	}
	public static void write() throws FileNotFoundException, IOException{
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("g:/java/object.txt"));
		oos.writeObject(new Person("aaa",33));//把對象寫入到文件中
		oos.close();
	}
}
class Person implements Serializable{//注意這個對象類要實現Serializable序列化接口
	//此接口會爲該對象生成一個根據該對象的序列化id,若是本對象改變了,調用本對象的類要從新編譯才行
	//或者直接指定本對象的序列化id
	public static final long serialVersionUID = 42L;//爲本對象生成一個固定的系列化id,這樣本類改變的話,調用類就不用從新編譯了

	private String name;
	public static String country="";//注意這裏,靜態不能序列化,由於序列化的在對內存中,而靜態在方法區中
	private int age;
	//若是不想被序列化,但又不定義靜態,能夠用transient關鍵字修飾,保證被修飾的能夠在對內存中存在,但不能存放到文本文件中
	//如這樣定義就能夠了:private transient int ages;
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
	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;
	}
	
}


 

                                          --
相關文章
相關標籤/搜索