package demo.assemble;
import java.io.*;
import java.util.Properties;
import java.util.Set;
/*
* store() 用于生成一个配置文件
load() 加载一个配置i文件
注意:
1. 如果配置文件存在着中文,那么生成配置文件的时候要使用字符流,否则会出现乱码。
2. 如果需要修改配置文件的内容,应该先加载原本配置文件,然后再生成一个配置文件。
* */
class 集合框架_Hashtable子类Properties {//Properties<String, String>
public static void main(String[] args) {
// baseDemo();//基本方法 注意Map的基本方法都适用
// outputFile();//集合元素写入文件
inputFile();//集合元素从文件中获取
// load("后期设备的配置文件.properties");//模拟Properties中load方
// modifyFile("后期设备的配置文件.properties", "a", "200");//修改配置文件中的信息,若无该键,则添加该键值
}
/**
* 修改配置文件中的信息,若无该键,则添加该键值
*
* @param filePath 文件路径
* @param key 要修改值对应的键
* @param value 将要修改的值
*/
private static void modifyFile(String filePath, String key, String value) {
File file = new File(filePath);//根据路径获得该文件对象
if (!file.isFile()) {//如果该文件不存在或者不是文件,退出程序
System.exit(-1);
}
Properties prop = new Properties();
BufferedReader bufr = null;
BufferedWriter bufw = null;
try {
bufr = new BufferedReader(new FileReader(file));
//!!注意不要把 bufw = new BufferedWriter(new FileWriter(file));写到此行
// 输出流关联文件会将文件置空
prop.load(bufr);//获得配置文件的键值对
Set<String> keys = prop.stringPropertyNames();//获取key集
boolean flag = false;//此标记用于判断是否找到指定key并修改
for (String k : keys) {
if (key.equals(k)) {//如果找到要修改key,就将原value修改为新value
prop.setProperty(k, value);
flag = true;//表示已经找到指定key并修改
}
}
if (!flag) {//如果未找到指定key进行修改
prop.setProperty(key, value);//就添新的键值
}
bufw = new BufferedWriter(new FileWriter(file));
prop.store(bufw, "name + age");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufr != null) bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//模拟Properties中load方法
private static void load(String filePath) {
Properties prop = new Properties();
BufferedReader bufr = null;
try {
bufr = new BufferedReader(new FileReader(filePath));//传入读取文件路径
String line = null;
while ((line = bufr.readLine()) != null) {
if (!line.startsWith("#")) {//如果这行不是注释就取键值(startsWith 字符串开头为指定字符)
String[] arr = line.split("=");//切割得到键值(等号左边为key,右边为value)
prop.setProperty(arr[0], arr[1]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufr != null) bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
prop.list(System.out);
}
//集合元素从文件中获取
private static void inputFile() {
Properties prop = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream("后期设备的配置文件.properties");
prop.load(fis);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
prop.list(System.out);
}
//集合元素写入文件
private static void outputFile() {
Properties prop = new Properties();
prop.setProperty("我", "18");//不能使用中文,该类采用ISO-8859-1编码表,不过可以正常读取。应该采用字符流
prop.setProperty("b", "19");
prop.setProperty("c", "17");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("后期设备的配置文件.properties");
prop.store(fos, "name + age");//注释也不能使用中文
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//基本方法 注意Map的基本方法都适用
private static void baseDemo() {
Properties prop = new Properties();
//储存元素
prop.setProperty("a", "18");
prop.setProperty("b", "19");
prop.setProperty("c", "17");
//修改元素
prop.setProperty("a", "20");
//遍历
Set<String> keys = prop.stringPropertyNames();//获取key集
for (String key : keys) {
String value = prop.getProperty(key);//根据key获取值
System.out.println(key + ": " + value);
}
//多用于调试
prop.list(System.out);//将prop中键值对打印到控制台
}
}
集合框架Hashtable的子类Properties的常用方法
最新推荐文章于 2025-07-15 13:34:04 发布
本文档详细介绍了如何使用Java的Properties类进行配置文件的读写、修改操作,包括加载配置文件、修改配置文件内容、从文件中获取集合元素等核心方法。示例代码展示了在处理包含中文的配置文件时,如何避免乱码问题,以及如何实现配置信息的增删改查。
8503

被折叠的 条评论
为什么被折叠?



