对象操作流

116 阅读2分钟

「这是我参与11月更文挑战的第8天,活动详情查看:2021最后一次更文挑战

对象操作流 对象操作流的特点: 可以把对象以字节的形式写到本地文件,直接打开文件,是读不懂的,需要再次用对象操作流读到内存中。

对象操作流:

对象操作流: 对象操作流分为两类:对象操作输入流和对象操作输出流 对象操作输出流(对象序列化流):就是将对象写到本地文件中,或者在网络中传输对象 对象操作输入流(对象反序列化流):把写到本地文件中的对象读到内存中,或者接收网络中传输的对象。

// 如果想要这个类的对象能被序列化,那么这个类必须要实现一个接口,Serializable
// Serializable 接口的意义
// 称之为是一个标记性接口,里面没有任何的抽象方法。
// 只要一个类实现了这个Serializable接口,那么就表示这个类的对象可以被序列化。
public class User implements Serializable {
    private String username;
    private String password;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

public class ConvertedDemo3 {
    public static void main(String[] args) throws IOException {
        User user = new User("zhangsan","qwer");

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        oos.writeObject(oos);

        oos.close();
    }
}
public class ConvertedDemo4 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        User o = (User)ois.readObject();
        System.out.println(o);
        ois.close();
    }
}

对象操作流:

用对象序列化流序列化一个对象后,假如我们修改了对象所属的Javabean类,读取数据会不会出现问题呢?

会出现问题,会抛出 InvalidClassException异常

如果出问题了,如何解决呢?

给对象所属的类加一个serialVersionUID

如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?

给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程。

Properties

Properties概述:

是一个Map体系的集合类

Properties中有跟IO相关的方法。

键值对的数据类型基本都定义为字符串。


public class PropertiesDemo1 {
    public static void main(String[] args) {
        Properties prop = new Properties();
        // 增删改查
        prop.put("xxx","dddd");
        prop.put("1xxx","1dddd");
        prop.put("2xxx","2dddd");

        prop.remove("xxx");
        System.out.println(prop);

        prop.put("xxx","aaaa");
        System.out.println(prop);

        Object value = prop.get("xxx");
        System.out.println(value);

        Set<Object> keys = prop.keySet();
        for (Object key: keys) {
            Object valuea = prop.get(key);
            System.out.println(key + "=" + valuea);
        }

        Set<Map.Entry<Object,Object>> entries = prop.entrySet();
        for (Map.Entry<Object,Object> entry: entries) {
            Object key = entry.getKey();
            Object value3 = entry.getValue();
            System.out.println(key + "=" + value3);
        }
    }
}
public class PropertiesDemo2 {
    public static void main(String[] args) {
        Properties prop = new Properties();
        prop.setProperty("1","1");
        prop.setProperty("2","2");
        prop.setProperty("3","3");
        System.out.println(prop);

        String value = prop.getProperty("1");
        System.out.println(value);

        Set<String > keys = prop.stringPropertyNames();
        for (String key: keys) {
            String va = prop.getProperty(key);
            System.out.println(key + "=" +va);
        }
    }
}

image.png

public class PropertiesDemo3 {
    public static void main(String[] args) throws IOException {
        // 读取
        Properties prop = new Properties();
        FileReader fr = new FileReader("otheriomodule/prop.properties");
        // 调用完了load方法之后,文件中的键值对数据已经在集合中了。
        prop.load(fr);
        fr.close();
        System.out.println(prop);
    }
}

image.png


public class PropertiesDemo3 {
    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        prop.put("aaa","qqq");
        prop.put("111","222");
        FileWriter fw = new FileWriter("prop.properties")
        prop.store(fw,"");
        // prop.store(fw,null);
        fw.close();
    }
}