本文共 3743 字,大约阅读时间需要 12 分钟。
java.util.Properties 类继承自 Hashtable,用于存储持久化的键值对数据。每个键和值都以字符串形式存在。Properties类广泛应用于读取系统属性、加载配置文件等场景。
public Properties():创建一个空的属性列表。public Object setProperty(String key, String value):保存键值对。public String getProperty(String key):通过键获取对应的值。public Set<String> stringPropertyNames():获取所有键的集合。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 缓冲流(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(); }} Java提供了对象序列化机制,将对象转换为字节序列,便于持久化存储或传输对象信息。
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):写对象的字段信息。public ObjectInputStream(InputStream in)。public final Object readObject():读取对象。public final Object readFields(ObjectstreamConstants.CTClass) throws IOException:读取对象的字段。Serializable接口。serialVersionUID版本号需一致。ClassNotFoundException和InvalidClassException。序列化示例:
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/