博客
关于我
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/

你可能感兴趣的文章
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>
Objective-C实现factorial阶乘算法(附完整源码)
查看>>
Objective-C实现Fast Powering算法(附完整源码)
查看>>
Objective-C实现fenwick tree芬威克树算法(附完整源码)
查看>>
Objective-C实现FenwickTree芬威克树算法(附完整源码)
查看>>
Objective-C实现fft2函数功能(附完整源码)
查看>>
Objective-C实现fibonacci斐波那契算法(附完整源码)
查看>>
Objective-C实现FigurateNumber垛积数算法(附完整源码)
查看>>
Objective-C实现first come first served先到先得算法(附完整源码)
查看>>
Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
查看>>
Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
查看>>
Objective-C实现hamming numbers汉明数算法(附完整源码)
查看>>
Objective-C实现hanning 窗(附完整源码)
查看>>
Objective-C实现hanoiTower汉诺塔算法(附完整源码)
查看>>
Objective-C实现hardy ramanujana定理算法(附完整源码)
查看>>
Objective-C实现harris算法(附完整源码)
查看>>
Objective-C实现haversine distance斜距算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现Hopcroft算法(附完整源码)
查看>>