博客
关于我
java之 属性集 & 缓冲流 & 序列化流
阅读量:398 次
发布时间:2019-03-05

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

Java Properties类与缓冲流、序列化流的应用

0x01、属性集(Properties类)

Properties类概述

java.util.Properties 类继承自 Hashtable,用于存储持久化的键值对数据。每个键和值都以字符串形式存在。Properties类广泛应用于读取系统属性、加载配置文件等场景。

Properties类构造方法

  • public Properties():创建一个空的属性列表。

Properties类存储方法

  • public Object setProperty(String key, String value):保存键值对。
  • public String getProperty(String key):通过键获取对应的值。
  • public Set<String> stringPropertyNames():获取所有键的集合。

Properties类与流的结合

  • public void load(InputStream inStream):从字节输入流中读取键值对。
  • public void load(Reader reader):从字符输入流中读取键值对。

示例代码

public static void main(String[] args) throws IOException {    Properties properties = new Properties();    properties.setProperty("filename", "a.txt");    properties.setProperty("length", "209385038");    properties.setProperty("location", "D:\\a.txt");    System.out.println(properties);    System.out.println(properties.getProperty("filename"));    System.out.println(properties.getProperty("length"));    System.out.println(properties.getProperty("location"));    Set
strings = properties.stringPropertyNames(); for (String key : strings) { System.out.println(key + " -- " + properties.getProperty(key)); }}

输出结果

{filename=a.txt, length=209385038, location=D:\a.txt}a.txt209385038D:\a.txtfilename -- a.txtlength -- 209385038location -- D:\a.txt

0x02、缓冲流

缓冲流概述

缓冲流(BufferedInputStream/BufferedOutputStream)是一种对四个基本输入输出流(FileInputStream/FileOutputStream)的增强,通过缓冲区减少I/O操作次数,提高读写效率。

字节缓冲流

  • public BufferedInputStream(InputStream in):创建缓冲输入流。
  • public BufferedOutputStream(OutputStream out):创建缓冲输出流。

字节缓冲流示例

public class BufferedDemo {    public static void main(String[] args) throws Exception {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("B_test.exe"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E_test.exe"));        int len;        while ((len = bis.read()) != -1) {            bos.write(len);        }        bos.close();        bis.close();    }}

字节数组缓冲流

public class BufferedDemo {    public static void main(String[] args) throws FileNotFoundException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("B_test.exe"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E_test.exe"));        byte[] bys = new byte[8192];        int len;        while ((len = bis.read(bys)) != -1) {            bos.write(bys, 0, len);        }        bos.close();        bis.close();    }}

0x03、序列化流

Java序列化概述

Java提供了对象序列化机制,将对象转换为字节序列,便于持久化存储或传输对象信息。

ObjectOutputStream类

  • 构造方法:public ObjectOutputStream(OutputStream out)
  • 方法:
    • public final void writeObject(Object obj):将对象写出到流中。
    • public final void writeObjectOverride(Object obj, String className):自定义写对象方法。
    • public final void writeFields(Object obj, String className, String[] fields):写对象的字段信息。

ObjectInputStream类

  • 构造方法:public ObjectInputStream(InputStream in)
  • 方法:
    • public final Object readObject():读取对象。
    • public final Object readFields(ObjectstreamConstants.CTClass) throws IOException:读取对象的字段。

序列化注意事项

  • 必须实现Serializable接口。
  • 对应的serialVersionUID版本号需一致。
  • 反序列化时需找到对应的类文件,避免ClassNotFoundExceptionInvalidClassException

示例代码

序列化示例:

public class Test {    public static void main(String[] args) throws Exception {        Student s = new Student("张三", 18);        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.txt"));        oos.writeObject(s);        oos.close();    }}反序列化示例:public class Test {    public static void main(String[] args) throws Exception {        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ser.txt"));        Object obj = ois.readObject();        System.out.println(obj);        ois.close();    }}

反序列化注意事项

  • 类文件版本号一致。
  • 类中无可访问的无参数构造方法。
  • 避免类中包含未知数据类型。

通过以上内容,可以看到Properties类与缓冲流、序列化流的实际应用场景。

转载地址:http://dxxzz.baihongyu.com/

你可能感兴趣的文章
Plotly:如何向烛台图添加交易量
查看>>
Plotly:如何在 plotly express 中找到趋势线的系数?
查看>>
Plotly:如何在桑基图中设置节点位置?
查看>>
Plotly:如何处理重叠的颜色条和图例?
查看>>
Plotly:如何手动设置 plotly express 散点图中点的颜色?
查看>>
Plotly:如何结合 make_subplots() 和 ff.create_distplot()?
查看>>
Plotly:如何绘制累积的“步骤“;直方图?
查看>>
Quartz进一步学习与使用
查看>>
Plotly条形图-根据正/负值更改颜色-python
查看>>
PLSQL developer12安装图解
查看>>
PLSQL Developer调试 存储过程和触发器
查看>>
PLSQL window操作
查看>>
plsql 存储过程 测试
查看>>
plsql 安装后database下拉没有东西
查看>>
PLSQL_Oracle PLSQL内置函数大全(概念)
查看>>
PLSQL_案例优化系列_体验逻辑结构如何影响SQL优化(案例3)
查看>>
PLSQL中INDEX BY TABLE的 DELETE操作
查看>>
plsql学习笔记---plsql相关概念,以及基础结构
查看>>
plsql数据库异常---plsql 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致
查看>>
plsql查询乱码问题解决
查看>>