当前位置:网站首页>Properties类和properties配置文件的理解学习
Properties类和properties配置文件的理解学习
2022-07-27 06:32:00 【爱学习的大雄】
引言
可能当我们的java程序正在运行时,我们需要修改其中配置的某一个值,但是我们又不可能将程序停止后修改其中的值,这个时候,就体现出使用properties配置文件的好处了。
如果该值是处properties配置文件中的,其可以很好很方便的跟java程序进行交互,我们不需要停止程序,可以直接修改properties配置文件中的值,那么java程序中的该值即可改变。
而Properties类,是java程序中读取properties配置文件的优选类,所以我们有必要去掌握它!
基本介绍
该类父类是HashTable,底层原理也是HashTable

该类是专门用于读写properties配置文件的集合类,配置文件格式如下:
键=值
键=值
**注意:**键值对不需要有空格,值不需要用引号一起来,默认类型是String
常用方法
load:加载配置文件的键值对到Properties对象list:将数据显示到指定设备getProperty(key):根据键获取值setProperty(key,value):设置键值对到Properties对象store:将Properties中的键值对存储到配置文件,在idea中保存信息到配置文件,如果含有中文,会存储为unicode码
需求编码
需求1:读取配置文件内容
如下一个配置文件mysql.properties
ip=0.0.0.0
user=root
pwd=root
在java程序中去读取其值
传统方式实现
使用字符流的方式去读取
/** * @author:雷子杰 * @date:2022/7/27 */
public class Properties01 {
public static void main(String[] args) throws IOException {
//获取当前工作目录,后面file的相对路径根据这个来写
//System.out.println(System.getProperty("user.dir"));//E:\Allworkspaces\idea-workspace\JavaEE
File file = new File("basic_review\\src\\properties_\\mysql.properties");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while((line = bufferedReader.readLine())!=null){
//分割读取出来的字符串
String[] split = line.split("=");
System.out.println(split[0]+"值是:"+split[1]);
//下面这个是输出全部内容
//System.out.println(line);
}
bufferedReader.close();
}
}

使用Properties类读取
使用Properties类读取配置文件中内容
/** * * 使用Properties来读取文件 * * @author:雷子杰 * @date:2022/7/27 */
public class Properties02 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//获取当前工作目录,后面file的相对路径根据这个来写
//System.out.println(System.getProperty("user.dir"));//E:\Allworkspaces\idea-workspace\JavaEE
//加载指定配置文件,相对路径为当前工作目录的相对路径
properties.load(new FileReader("basic_review\\src\\properties_\\mysql.properties"));
//将k-v显示控制台
properties.list(System.out);
System.out.println("=====================");
//根据key获取对于的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名:"+user);
System.out.println("密码:"+pwd);
}
}

需求2:添加配置文件内容
将Properties类创建的键值对写入新文件中
/** * * 使用properties类来创建配置文件 * * @author:雷子杰 * @date:2022/7/27 */
public class Properties03 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//创建
properties.setProperty("user","lzj");//如果用户名为中文,则会保存unicode码
properties.setProperty("pwd","123456");
properties.setProperty("age","22");
//存储至文件中
//形参中的comments为null即可,如果添加即为添加注释
properties.store(new FileOutputStream("basic_review\\src\\properties_\\mysql2.properties"),null);
System.out.println("保存配置成功");
}
}

需求3:修改读取的配置文件
先读取,再修改配置写入原文件即可
/** * * 使用Properties来修改读取的文件 * * @author:雷子杰 * @date:2022/7/27 */
public class Properties04 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//获取当前工作目录,后面file的相对路径根据这个来写
//System.out.println(System.getProperty("user.dir"));//E:\Allworkspaces\idea-workspace\JavaEE
//加载指定配置文件,相对路径为当前工作目录的相对路径
properties.load(new FileReader("basic_review\\src\\properties_\\mysql.properties"));
//将k-v显示控制台
properties.list(System.out);
//修改user
properties.setProperty("user","lzj");
System.out.println("=====================");
//根据key获取对于的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名:"+user);
System.out.println("密码:"+pwd);
//将修改完成后的键值对写入文件中
properties.store(new FileOutputStream("basic_review\\src\\properties_\\mysql.properties"),null);
}
}

全部方法
| Modifier and Type | Method and Description |
|---|---|
String | getProperty(String key) 使用此属性列表中指定的键搜索属性。 |
String | getProperty(String key, String defaultValue) 使用此属性列表中指定的键搜索属性。 |
void | list(PrintStream out) 将此属性列表打印到指定的输出流。 |
void | list(PrintWriter out) 将此属性列表打印到指定的输出流。 |
void | load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)。 |
void | load(Reader reader) 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。 |
void | loadFromXML(InputStream in) 将指定输入流中的XML文档表示的所有属性加载到此属性表中。 |
Enumeration<?> | propertyNames() 返回此属性列表中所有键的枚举,包括默认属性列表中的不同键,如果尚未从主属性列表中找到相同名称的键。 |
void | save(OutputStream out, String comments) 已弃用 如果在保存属性列表时发生I / O错误,此方法不会抛出IOException。 保存属性列表的store(OutputStream out, String comments)方法是通过store(OutputStream out, String comments)方法或storeToXML(OutputStream os, String comment)方法。 |
Object | setProperty(String key, String value) 致电 Hashtable方法 put 。 |
void | store(OutputStream out, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法加载到 Properties表中的格式输出流。 |
void | store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式输出到输出字符流。 |
void | storeToXML(OutputStream os, String comment) 发出表示此表中包含的所有属性的XML文档。 |
void | storeToXML(OutputStream os, String comment, String encoding) 使用指定的编码发出表示此表中包含的所有属性的XML文档。 |
Set<String> | stringPropertyNames() 返回此属性列表中的一组键,其中键及其对应的值为字符串,包括默认属性列表中的不同键,如果尚未从主属性列表中找到相同名称的键。 |
边栏推荐
猜你喜欢

Overall dichotomy?

Using docker in MAC to build Oracle database server

一款开源 OA 办公自动化系统

Chapter 6 Shell Logic and Arithmetic

Generics -- learn it, and there are many benefits

Gossip: Recently, many friends talk about going abroad

C语言程序设计 | 程序编译与预处理

The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars

在mac中使用docker来搭建oracle数据库服务器

Internal class -- just read this article~
随机推荐
[Vani有约会]雨天的尾巴
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
C语言程序设计 | 程序编译与预处理
Oracle database problems
Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
高级IO提纲
vlan间路由(讲解+验证)
VLAN trunk实验
Introduction to network -- overview of VLAN and trunk
Oracle 组合查询
Use shell to calculate the sum of numbers in text
网络入门——vlan及trunk概述
Simple rotation chart
多线程【初阶-上篇】
LogCat工具
【golang学习笔记2.0】 golang中的数组和切片
Federal Reserve SR 11-7: Guidance on model risk management - Wanzi collection
[Vani has a date] tail on rainy days
用户解锁SM04 SM12
Drools (5): drools advanced syntax