1. 程式人生 > >Properties類、序列化流與反序列化流

Properties類、序列化流與反序列化流

Properties類
Properties類介紹

特點:
1、Hashtable的子類,map集合中的方法都可以用。
2、該集合沒有泛型。鍵值都是字串。
3、它是一個可以持久化的屬性集。鍵值可以儲存到集合中,也可以儲存到持久化的裝置(硬碟、U盤、光碟)上。鍵值的來源也可以是持久化的裝置。
4、有和流技術相結合的方法。
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
//鍵值對檔案 public class Demo08 { public static void main(String[] args) throws IOException { method1(); method2(); method3(); } public static void method1(){ Properties pro=new Properties(); //往集合中儲存鍵值對 pro.setProperty("b", "1"); pro.setProperty(
"a", "2"); //取值 System.out.println(pro.getProperty("a")); //相當於keySet方法 Set<String> set=pro.stringPropertyNames(); for(String s:set){ System.out.println(s+pro.getProperty(s)); } } public static void method2() throws IOException{ Properties pro
=new Properties(); FileReader fr=new FileReader("E:\\java\\wojiubu.properties"); pro.load(fr);//從檔案中讀取鍵值對 System.out.println(pro); } public static void method3() throws IOException{ Properties pro=new Properties(); pro.setProperty("a", "1"); pro.setProperty("b", "2"); pro.setProperty("c", "3"); pro.setProperty("c", "4"); //明確目的地 FileOutputStream fos=new FileOutputStream("E:\\java\\wojiubu.properties"); pro.store(fos, "hehe"); } } 序列化流與反序列化流 物件序列化流ObjectOutputStream


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
//寫入檔案
public class Demo01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //method1();
        method2();
    }
    public static void method1() throws IOException{
        //序列化
        Person p=new Person("alai",18);
        //明確目的地
        FileOutputStream fos=new FileOutputStream("E:\\java\\person.txt");
        //建立序列化流
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        //向檔案寫入物件
        oos.writeObject(p);
        oos.close();
    }
物件反序列化流ObjectInputStream


//讀取檔案
    public static void method2() throws ClassNotFoundException, IOException{
        //明確資料來源
        FileInputStream fis=new FileInputStream("E:\\java\\person.txt");
        //建立反序列化流
        ObjectInputStream ois=new ObjectInputStream(fis);
        Object obj=ois.readObject();
        Person p=(Person)obj;
        System.out.println(p);
        ois.close();
    }
}
瞬態關鍵字transient
import java.io.Serializable;

public class Person implements Serializable{
    private String name;
    //瞬態關鍵字 transient  
    private transient int age; //新增瞬態關鍵字的屬性不會被序列化
    //自定義的序列化號
    private static final long serialVersionUID = 42L;
    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 String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public Person(){
        
    }
}