Properties,序列化

1    Properties类介绍

Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

特点:

1、Hashtable的子类,map集合中的方法都可以用。

2、该集合没有泛型。键值都是字符串。

3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。

4、有和流技术相结合的方法。

public class Demo01 {
    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.setProperty("name", "zhangsan");
        pro.setProperty("age", "123");
        System.out.println(pro.getProperty("age"));
        // 遍历
        Set<String> set = pro.stringPropertyNames();
        for(String key:set){
            System.out.println(key+pro.getProperty(key));
        }
    }
}

1   从文件中读取键值对到集合中

public class Demo02 {
    public static void main(String[] args) throws IOException {
        Properties pro=new Properties();
        //明确数据源
        FileInputStream fis=new FileInputStream("D:\test\pro.properties");
        //从文件中读取键值对到集合中
        pro.load(fis);
        System.out.println(pro);
    }
}

2   把集合中的数据存储到流所对应的文件中

public class Demo03 {
    public static void main(String[] args) throws IOException {
        Properties pro=new Properties();
        //明确目的地
        FileOutputStream fos=new FileOutputStream("D:\test\pro2.properties");
        //准备数据
        pro.setProperty("name", "lisi");
        pro.setProperty("age", "123");
        pro.store(fos, "");
    }
}

2 序列化与反序列化

用于从流中读取对象的

操作流 ObjectInputStream    称为 反序列化流

用于向流中写入对象的操作流 ObjectOutputStream   称为 序列化流

l  特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。

写入对象:

public class Demo01 {
    public static void main(String[] args) throws IOException {
        Person p=new Person("小红帽",18);
        //明确目的地
        FileOutputStream fos=new FileOutputStream("D:\test\person.txt");
        //创建序列化流
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        //写入对象
        oos.writeObject(p);
        //释放资源
        oos.close();
    }
}

读取对象:

public class Demo02 {
    public static void main(String[] args) throws ClassNotFoundException, IOException {
        //明确数据源
        FileInputStream fis=new FileInputStream("D:\test\person.txt");
        //创建反序列化流
        ObjectInputStream ois=new ObjectInputStream(fis);
        //读对象
        Object obj=ois.readObject();
        System.out.println(obj);
        //释放资源
        ois.close();
    }
}

3      序列化接口

当一个对象要能被序列化,这个对象所属的类必须实现Serializable接口。否则会发生异常NotSerializableException异常。

4       瞬态关键字transient

当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了,序列化时这个属性就不会序列化了。

public class Person implements Serializable{
    private String name;
    private int age;
    //瞬态关键字transient,被它修饰的成员变量,不会被序列化
    private static final long serialVersionUID=123L;
    public Person() {
        super();
    }
    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;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    
}
原文地址:https://www.cnblogs.com/quanjunkang/p/10656021.html