Properties 集合
2024-08-27 18:45:40
- Map
- Hashtable
- Properties
- Hashtable
- 特点:
- 该集合中的键和值都是字符串类型
- 集合中的数据可以保存到流中, 或者从流中获取
- 应用:
- 通常该集合用于操作以键值对形式存在的配置文件
- 常用方法:
// 单个元素的存和取:
// 存元素
setProperty();
// 取元素
getProperty();
// 获取所有元素 (将 Map 集合转换成 Set 集合)
Set<String> stringPropertyNames(); // 返回此属性列表中的键集
// 示例:
public static void propertiesDemo(){
// 创建一个 Properties 的集合
Properties prop = new Properties();
// 存储元素, 注意, 键和值都是字符串
prop.setProperty("wangcai","22");
prop.setProperty("lisi","32");
prop.setProperty("zhangsan",35);
// 修改元素, 键相同, 值覆盖
prop.setProperty("lisi","14");
// 获取所有元素
Set<String> names = prop.stringPropertyNames();
for(String name:names){
String value = prop.getProperty(name);
System.out.println(name+":"+value);
}
}
Properties 集合和流对象相结合的方法
// list(PrintStream out);
public static void method(){
Properties prop = new Properties();
prop.setProperty("wangcai","18");
prop.setProperty("xiaoqiang","28");
prop.setProperty("zhaoliu","25");
prop.list(System.out); // 将集合信息打印到控制台
}
// 将 Properties 表中的属性列表写入到输出流:
// store(OutputStream out, String comments(属性列表的描述));
// store(Writer writer, String comments);
public static void method_1(){
Properties prop = new Properties();
prop.setProperty("wangcai","18");
prop.setProperty("xiaoqiang","28");
prop.setProperty("zhaoliu","25");
// 想要将这些集合中的字符串键值信息持久化存储到文件中
// 需要关联输出流
FileOutputStream fos = new FileOutputStream("info.txt");
// 将集合中数据存储到文件中, 使用 store() 方法
prop.store(fos,"name+age");
fos.close(); // 关闭流
}
// 将硬盘上已有的文件信息读取到 Properties 集合中
// load(InputStream in); 字节流
// load(Reader reader); 字符流
public static void method_2(){
Properties prop = new Properties();
// 集合中的数据来自与一个文件
// 注意: 必须要保证该文件中的数据是键值对
// 需要使用到读取流(字节流或字符流)
FileInputStream fis = new FileInputStream("info.txt");
// 使用 load() 方法
prop.load();
fis.close();
}
// 模拟实现 load() 方法
public static void MyLoad(){
Properties prop = new Properties();
BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
String line = null;
while((line=bufr.readLine())!=null){
// 将 info.txt 中的注释文件过滤出去
if(line.startsWith("#"))
continue;
// 使用 "=" 切割字符串
String[] arr = line.split("=");
// 存入 Properties 集合中
prop.setProperty(arr[0],arr[1]);
}
bufr.close();
}
// 示例二: 对已有的配置文件中的信息进行修改
/*
* 思路:
* 读取这个文件
* 将这个文件中的键值数据存储到集合中
* 通过集合对数据进行修改
* 在通过流将修改后的数据存储到文件中
*/
public static void test() throws IOException{
// 首先将文件封装成对象, 判断该文件是否存在
// 如果不存在, 则创建一个新的
File file = new File("info.txt");
if(!file.exists()){
file.createNewFile();
}
// 读取该文件, 流可以直接操作文件对象
FileReader fr = new FileReader(file);
// 将文件中的键值数据存储到集合中
Properties prop = new Properties();
prop.load(fr);
// 修改数据
prop.setProperty("zhaoliu","30");
// 将修改后的 Properties 存储到文件中
FileWriter fw = new FileWriter(file);
prop.store(fw,"changeName"); // 这是将 info.txt 中的内容全部覆写一遍
// 并不是仅仅修改了 zhaoliu
// store() 方法需要两个参数
fr.close();
fw.close();
}
参考资料
最新文章
- [Java IO]04_系统标准IO
- 深入理解javascript原型和闭包(完结)
- LeetCode 2. Add Two Numbers swift
- 初识Memcached
- Count and Say
- idea自动生成serialVersionUID
- Oracle查询被锁的表及进程的方法
- ubuntu 12.04 编译安装 nginx
- 【转】iOS 解决ipv6问题
- HTML5 Canvas Arc Tutorial
- 前端资料QQ群交流
- JavaScript之基础语法整理
- 软件包管理(rpm,yum)
- 几种事务的隔离级别,InnoDB如何实现?
- kepware http接口 c语言 ruby
- css的overflow属性
- c运行库、c标准库、windows API的区别和联系
- spark shuffle内在原理说明
- python自动化之读写
- OleVariant Variant
热门文章
- atitti.atiNav 手机导航组件的设计
- Atiti. Php Laravel 5.1 环境搭建以及 &#160;error 排除
- Java中的synthetic
- UITableviewcell的性能问题
- json-lib解析json之二维JSONArray
- CentOS6.2下安装Qt5.1.0
- The Definitive Guide To Django 2 学习笔记(三) URLconfs 和松耦合
- ubus
- java文件对话框操作
- 将字符“12345”转换成long型