序列化和反序列化,Properties工具类

一、序列化和反序列化

对象实现序列化后,可以进行网络传输或存储磁盘的功能。本质是,序列化就是将对象变成byte[]形式;反序列化 就是将byte[] 变成对象。

对象要实现序列化,类一定要实现Serializable接口

ObjectInputStream和ObjectOutputStream

直接读写对象,前提是对象的类实现Serializable接口

    public static void main(String[] args) {
        //将一个Person对象存储到temp.txt中
        Person p = new Person("tom",20);
        try {
            FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113/temp.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(p);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
//读取temp.txt中存储的对象
try {
        FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113/temp.txt");
        ObjectInputStream bis = new ObjectInputStream(fis);
        Person p = (Person)bis.readObject();
        System.out.println(p.getName()+","+p.getAge());
        } catch (FileNotFoundException e) {

        e.printStackTrace();
        } catch (IOException e) {

        e.printStackTrace();
        } catch (ClassNotFoundException e) {

        e.printStackTrace();
        }

二、对象进行序列化和反序列化的封装

获得对象的byte[] 及 将byte[] 变成对象 ;封装两个方法

需要使用ByteArrayInputStream和ByteArrayOutputStream ,是对byte[]的读写的流

public class Test {
    public static void main(String[] args) {
        Person p = new Person("tom",22);
        Person p2 = new Person("jack",21);
        Person p3 = new Person("smith",20);
        List<Person> list = new ArrayList<Person>();
        list.add(p);
        list.add(p2);
        list.add(p3);
        byte[] bytes = getBytes(list);
        List<Person> ps = (List<Person>)getObject(bytes);
        for(Person person:ps) {
            System.out.println(person.getName()+","+person.getAge());
        }
    }
    /**
     * 将对象序列化成byte[]
     * @param obj
     * @return
     */
    public static byte[] getBytes(Object obj) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            return baos.toByteArray();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }
    /**
     * 将byte[] 反序列化成对象
     * @param bytes
     * @return
     */
    public static Object getObject(byte[] bytes) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }
}

三、Properties工具类

用来读取Properties配置文件的一个工具类 ,Java中的配置文件包括了.properties和.xml形式的配置文件, properties配置文件编写和读取相对简单,是以key=value的格式编写的 ,但不能表示数据的结构化 ; xml形式的 配置文件编写和读取相对复制,表示数据的结构化

Properties继承于Hashtable ,Properties会将配置信息读取到Hashtable中,通过key获得value

编程实现读取配置文件

public class TestProperties {
    public static void main(String[] args) {
        Properties p = new Properties();
        try {
            //将配置文件内容读取到 Properties 的Hashtable中
            FileInputStream fis = new
                    FileInputStream("D:\\MyWork\\MyWorkspaces\\eclipse0106\\pjt0211\\src\\test.properties");
            p.load(fis);
            System.out.println(p.getProperty("username"));
            System.out.println(p.getProperty("password"));
            //向Hashtable中设置内容
            p.setProperty("sfz", "1001100");
            p.store(new
                        FileOutputStream("D:\\MyWork\\MyWorkspaces\\eclipse0106\\pjt0211\\src\\test.properties"), "***");
                    System.out.println(p.getProperty("sfz"));
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
public static void main(String[] args) {
        //类路径 (classPath) .class的编译后的文件目录
        ResourceBundle bundle = ResourceBundle.getBundle("test");
        System.out.println(bundle.getString("username"));

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aigo-2021

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值