Java Object流和Propertise流

时间:2022-07-22
本文章向大家介绍Java Object流和Propertise流,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.序列化与反序列化

public class ObjectOutputStreamDemo {
  /**
   * 序列化流实质是使对象写入文件,或者在网络中传输
   * 把对象按照流一样的方式存入文本文件,或者在网络中传输 --写--序列化
   * 反序列化,就是把文本文件中的流对象或者网络中的流对象还原成对象 --读--反序列化
   */
    public static void main(String[] args) {
        try {
            write();
            try {
                read();
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        
    }

private static void read() throws IOException, Throwable {
      //创建反序列化对象
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));

    Object obj =    ois.readObject();
    //释放资源
    ois.close();
    System.out.println(obj);
}

private static void write() throws IOException {
    //创建序列化对象
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
    
    //创建被序列化类对象
    Student stu = new Student("张晓天",18,"男");
    
    oos.writeObject(stu);
    //释放资源
    oos.close();
    
}
}

如果类中的成员不想被序列化 可以在前面加 transient 关键字

2.属性类

public class PropertiseDemo {
        public static void main(String[] args) {
            //作为map集合使用
            Properties prop = new Properties();
            //添加元素
            prop.put("itoo1", "zhang");
            prop.put("it002", "xiao");
            prop.put("it003", "tian");
            
            //遍历map集合
            Set<Object> set = prop.keySet();
            
            for (Object key : set) {
                Object value =prop.get(key);
                System.out.println(key+"---"+value);
            }
            System.out.println("prop:"+prop);
        }
}

3.复制文件

public static void main(String[] args) throws IOException {
         Files.copy(Paths.get("a.java"),new FileOutputStream("c.java"));
    }

4.集合写入文件

 public static void main(String[] args) throws IOException {
         
         ArrayList<String> arrays = new ArrayList<String>();
         arrays.add("hello");
         arrays.add("word");
         arrays.add("java");
         Files.write(Paths.get("a.txt"), arrays, Charset.forName("GBK"));
    }

5.Properties修改属性文件中的键值对

class PropertiesDemo {
    /**
     * 1.把文件中的数据加载到集合中
     * 2.遍历集合获取每一个键
     * 3.判断是否有“lisi”的键,如果有就修改其值为“100”
     * 4.把集合中的数据重新存储到文件中
     *如果出现错误可能因为导包的原因
     */
     public static void main(String[] args) throws IOException {
         //把属性文件中的数据加载到集合
         Properties prop = new Properties();
         Reader r = new FileReader("a.txt");
         prop.load(r);
         r.close();
         //遍历集合获取每一个键
         Set<String> set = prop.stringPropertyNames();
         for (String string : set) {
            if("lisi".equals(string)){
                prop.setProperty(string, "100");
                break;
            }
        }
         //把集合中的数据重新存储到文件中
         Writer w = new FileWriter("a.txt");
         prop.store(w,null);
         w.close();
         
        
    }

}