博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Properties类、序列化流与反序列化流
阅读量:4659 次
发布时间:2019-06-09

本文共 3275 字,大约阅读时间需要 10 分钟。

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
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();    }}瞬态关键字transientimport 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(){            }}

 

转载于:https://www.cnblogs.com/zhaotao11/p/10248186.html

你可能感兴趣的文章