比如说
1.我们需要将配置文件信息读取
到程序中
2.另外一种我们在程序中修改了信息再写回
进配置文件中Properties中 我们很多东西是在配置文件中写的 而不是写死在程序 程序操作数据库DB1和DB2 需要用户名和密码的 如果写死在程序 切换到DB2那么意味着要修改源码程序的 也就是源码都得修改且得重新编译 一般程序运行在客户 客户无动手能力去修改源码的,且修改起来成本代价高 我们将相关信息写入配置文件 这样完成解耦
这里是创建一个管道 从硬盘读取到内存 也就是缓冲区的流文件罢了
这里load显示一个大read和getProperty完成对指定键值对读取共同完成就是我们的read罢了
store才是write
这里是左侧的管道流进行读取
package com.ReviewProperties.java;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//传统方法
public class Properties01 {
public static void main(String[] args) throws IOException {
//读取mysql.properties文件 并获得ip
BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\main\\java\\com\\ReviewProperties\\mysql.properties"));
String line="";
while ((line=bufferedReader.readLine())!=null){ //循环读取人
String[] split=line.split("=");
//如果我们要求指定的ip值
if("ip".equals(split[0])) {
System.out.println(split[0] + "值是:" + split[1]);
}
};
bufferedReader.close();
}
}
ip值是:192.168.100
这里我们可以看到我们得自己去书写过滤条件equal方法 很麻烦。每次获取新需求我们得自己写过滤条件 所以我们可以通过Properties方便实现这个方法
Properties类介绍
Properties专门读写文件集合的类 不能乱写 只能写成
1.键=值
这种形式 2.默认类型为String
类型
对于setProperty 如果没有这个键值对 就相当于默认添加了这个键值对 对于store 方法 如果这个键值对已经在配置文件中相当于覆盖
//properties是中间缓冲区
//1)专门用于读写配置文件的集合类
// 配置文件的格式:
// 键=值
// 键=值
//2)注意:键值对不需要有空格,值不需要用引号一起来。默认类型是String
//3)Properties的常见方法
// 1.load:加载配置文件的键值对到Properties对象
// 2.list:将数据显示到指定设备/流对象
// 3. getProperty(key):根据键获取值
// 4.setProperty(key,value)设置键值对到Properties对象 如果没有key就新创建
// 5. store:将Properties中的键值对存储到配置文件相当于创建一个文件如果已经存在就覆盖,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
/*应用案例
I.使用Properties类完成对mysql..properties的读取,看老师代码演示
2.使用Properties类添加key-val到新文件mysql2.properties中
3.使用Properties类完成对mysql..properties的读取,并修改某个key-val*/
public class Properties02 {
public static void main(String[] args) throws IOException {
//1.创建Properties对象
Properties properties1=new Properties();
//2。加载指定配臀文件
properties1.load(new FileReader("src\\main\\java\\com\\ReviewProperties\\mysql.properties"));
//3.把k-v显示到控制台
properties1.list(System.out);
//4.根据key 获取对应的值
String user=properties1.getProperty("user");
String pwd=properties1.getProperty("pwd");
System.out.println("用户名"+user);
System.out.println("密码"+pwd);
}
}
– listing properties –
user=root
pwd=12345
ip=192.168.100
用户名root
密码12345
public static void main(String[] args) throws IOException {
//使用Properties类创建配置文件 修改配置文件内容
Properties properties=new Properties();
//创建 现在简直对在内存里里面
//1.如果该文件没有key就是创建
//2.如果该文件有key,就是修改
/**
* Properties父类是Hashtable 底层就是Hashtable 核心方法
* public synchronized V put(K key, V value) {
* // Make sure the value is not null
* if (value == null) {
* throw new NullPointerException();
* }
*
* // Makes sure the key is not already in the hashtable.
* Entry,?> tab[] = table;
* int hash = key.hashCode();
* int index = (hash & 0x7FFFFFFF) % tab.length;
* @SuppressWarnings("unchecked")
* Entry entry = (Entry)tab[index];
* for(; entry != null ; entry = entry.next) {
* if ((entry.hash == hash) && entry.key.equals(key)) {
* V old = entry.value;
* entry.value = value; //如果key存在就替换
* return old;
* }
* }
*
* addEntry(hash, key, value, index); 如果是新k 就add
* return null;
* }
*/
properties.setProperty("charset","utf8");
properties.setProperty("user","tom汤姆"); //注意保存的是 中文的unicode码
properties.setProperty("pwd", "9999");
//将k-v存储文件中即可
properties.store(new FileOutputStream("src\\main\\java\\com\\ReviewProperties\\mysql2.properties"), "hello world");
//第二个参数仅仅是一个注释
System.out.println("保存配置文件成功");
}
}
package com.ReviewProperties.java;
// 1.load:加载配置文件的键值对到Properties对象
// 2.list:将数据显示到指定设备/流对象
// 3. getProperty(key):根据键获取值
// 4.setProperty(key,value)设置键值对到Properties对象 如果没有key就新创建
// 5. store:将Properties中的键值对存储到配置文件相当于创建一个文件如果已经存在就覆盖,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Properties03 {
public static void main(String[] args) throws IOException {
//使用Properties类创建配置文件 修改配置文件内容
Properties properties=new Properties();
//创建 现在简直对在内存里里面
//1.如果该文件没有key就是创建
//2.如果该文件有key,就是修改
/**
* Properties父类是Hashtable 底层就是Hashtable 核心方法
* public synchronized V put(K key, V value) {
* // Make sure the value is not null
* if (value == null) {
* throw new NullPointerException();
* }
*
* // Makes sure the key is not already in the hashtable.
* Entry,?> tab[] = table;
* int hash = key.hashCode();
* int index = (hash & 0x7FFFFFFF) % tab.length;
* @SuppressWarnings("unchecked")
* Entry entry = (Entry)tab[index];
* for(; entry != null ; entry = entry.next) {
* if ((entry.hash == hash) && entry.key.equals(key)) {
* V old = entry.value;
* entry.value = value; //如果key存在就替换
* return old;
* }
* }
*
* addEntry(hash, key, value, index); 如果是新k 就add
* return null;
* }
*/
properties.setProperty("charset","utf8");
properties.setProperty("user","tom汤姆"); //注意保存的是 中文的unicode码
properties.setProperty("pwd", "9999");
//将k-v存储文件中即可
properties.store(new FileOutputStream("src\\main\\java\\com\\ReviewProperties\\mysql2.properties"), "hello world");
//第二个参数仅仅是一个注释
System.out.println("保存配置文件成功");
}
}
输出结果
对于这个store方法的第二个参数 他会显示在mysql2.properties文件的最上方