当前位置: 首页 > news >正文

properties配置和读取

如何配置和读取属性文件

  • 1.属性文件介绍
    • 1.1 什么是属性文件
    • 1.2属性文件规范
    • 1.3 属性文件优缺点
  • 2.属性文件读取
  • 4.spring和属性文件
    • 4.1利用注解读取
    • 4.2配置文件里直接引用
  • 4.属性文件写入
  • 5.注意事项
  • 5.总结

1.属性文件介绍

1.1 什么是属性文件

Java开发中,我们经常需要读取和写入配置文件,用来存储程序中的一些配置信息,例如数据库的连接信息、邮件和Web服务器的信息、消息队列的信息等等。配置文件一般都是key-value形式,且它的key-value一般都是String-String类型的,因此我们完全可以用Map<String, String>来表示它。

但因为配置文件特别常用,所以Java集合库给我们提供了一个Properties类来表示一组“配置”,专门用来处理key-value形式的配置信息。Properties类可以表示一个持久的属性集,每个键及其对应的值都是字符串类型,它可以把配置信息保存在一个IO流中,或是从一个IO流中加载配置信息,因此很适合用来处理配置文件。

Properties的内部本质上是一个Hashtable,该类从Hashtable中继承了get()和put()方法,这些方法的参数签名是Object。但由于历史遗留原因,Properties的设计实际上是有问题的,不过为了保持兼容性,现在已经没法修改了。所以我们在使用Properties时,不要去调用这些从Hashtable继承来的方法,而应该使用Properties自身关于读写配置的方法,比如getProperty()和setProperty()等方法。

1.2属性文件规范

1.属性文件都是以键值对出现
key=value
2.注释用#开头
3.增加可读性,key很多时候都用xx.xxx表示

eg:

#log4j属性配置
log4j.rootLogger=DEBUG,A
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n# properties文件示例
# 注释内容以#号开头
name=Tom
age=22
gender=Male

1.3 属性文件优缺点

1、Properties文件的优点:

(1)Properties文件结构简单,易于读写、解析。

(2)Properties文件的扩展性强,可以随时添加、修改、删除属性。

(3)Properties文件可以存储键值对类型的数据,非常通用。

(4)Properties文件具有跨平台性,可以在不同操作系统上使用。

2、Properties文件的缺点:

(1)Properties文件对复杂结构和大量数据的支持不够好。

(2)Properties文件格式和内容没有规范,容易产生格式错误、解析异常等问题。

3、注意事项:

(1)Properties文件不是加密文件,存储敏感信息时建议进行加密处理。

(2)Properties文件的编码一般使用ISO-8859-1,建议不要使用Unicode编码。

2.属性文件读取

属性文件本身是一个文件,要读取一个文件,需要获得这个文件的InputStream。
在利用Properties类起封装文件流,就可以用getProperty(key)读取属性值

java提供了多种方式,如果属性文件放置在类目录下,用 ClassLoader.getSystemResourceAsStream是最推荐的。其中最容易出错的是文件路径的定位

方法说明
this.getClass().getClassLoader().getResourceAsStream(fileName)默认从class根目录,不加/
this.getClass().getResourceAsStream(fileName)默认当前class目录,从根目录算,要加/
ClassLoader.getSystemResourceAsStream(fileName)默认从class根目录,不加/
new FileInputStream(fileName)绝对路径,不推荐
reader = new FileReader(fileName)绝对路径,不推荐
Resource resource = new ClassPathResource(fileName);等价ClassLoader
ResourceBundle rb2 = ResourceBundle.getBundle(fileName);等价ClassLoader
package com.jsoft.test;import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;/*** @class: com.jsoft.test.PropertiesTest* @description:* @author: jiangzengkui* @company: 教育家* @create: 2023-12-07 23:07*/
public class PropertiesTest {/*** 1. 方式一* 从当前的类加载器的this.getClass().getResourcesAsStream来获取* InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name)* 前提:属性文件必须放置在类的目录结构里* 文件路径判断:*   this.getClass().getResourceAsStream是从当前类去寻找资源*   1.如果不加路径,则从当前class类的目录里找*   2.如果有路径,但没有根路径,则从当前类的子目录找*   eg:*   当前类:com.jsoft.MyProper*   this.getClass().getClassLoader().getResourceAsStream("user/user.properties")*   是寻找com.jsoft.user类目录里是否有属性文件* @throws IOException* 3.如果有路径,而且有根路径,则从classes根目录找* this.getClass().getResourceAsStream("/com/jsoft/test/user/user1.properties");*/@Testpublic void t1() throws IOException {InputStream inputStream=null;inputStream = this.getClass().getResourceAsStream("user.properties");inputStream = this.getClass().getResourceAsStream("user/user.properties");inputStream = this.getClass().getResourceAsStream("/com/jsoft/test/user/user1.properties");inputStream = this.getClass().getResourceAsStream("/log4j.properties");printProper(inputStream);}/*** 2. 方式二* this.getClass().getClassLoader()是获得java类加载器ClassLoader* ClassLoader.getResourceAsStream默认就是从根目录加载,路径不能从/开始* 如:log4j.properties就表示从class根目录了,加/log4j.properties反而出错* @throws IOException*/@Testpublic void t2() throws IOException {InputStream inputStream=null;//放在在class根目录inputStream = this.getClass().getClassLoader().getResourceAsStream("log4j.properties");//放置在classpath/com/jsoft/testinputStream = this.getClass().getClassLoader().getResourceAsStream("com/jsoft/test/user.properties");//放置在classpath/com/jsoft/test/userinputStream = this.getClass().getClassLoader().getResourceAsStream("com/jsoft/test/user/user1.properties");printProper(inputStream);}/*** 3.方式三:* 直接用类加载器的静态方法* ClassLoader.getSystemResourceAsStream()* 等价于* this.getClass().getClassLoader().getResourceAsStream()* 对路径的要求也是一样,都是不需要加根路径/* @throws IOException*/@Testpublic void t3() throws IOException {InputStream inputStream=null;//放在在class根目录inputStream = ClassLoader.getSystemResourceAsStream("log4j.properties");//放置在classpath/com/jsoft/testinputStream = ClassLoader.getSystemResourceAsStream("com/jsoft/test/user.properties");//放置在classpath/com/jsoft/test/userinputStream = ClassLoader.getSystemResourceAsStream("com/jsoft/test/user/user1.properties");printProper(inputStream);}/*** 4.方法四* 用FileInputStream(文件)获取文件流,这种方法:* 1.取工程的路径* 2.取绝对路径* 这个方法适用于属性文件没有放置classses目录下,用绝对路径可访问,原则不推荐* @throws IOException*/@Testpublic void t4() throws IOException {InputStream inputStream=null;inputStream=new FileInputStream("src/main/resources/log4j.properties");inputStream=new FileInputStream("target/classes/log4j.properties");inputStream=new FileInputStream("D:/java/idea_project/mybatis/basic/target/classes/log4j.properties");printProper(inputStream);}/*** 5. 方法五* 使用 FileReader reader = new FileReader(fileName)* 和FileInputStream一样,也需要绝对路径,不推荐* @throws IOException*/@Testpublic void t5()throws IOException {FileReader reader = new FileReader("D:/java/idea_project/mybatis/basic/target/classes/log4j.properties");Properties properties=new Properties();properties.load(reader);printProper(properties);}/*** 6.方法六* spring提供的帮助类* 和ClassLoader一样,不需要根路径*/@Testpublic void t6() throws IOException {Resource resource = new ClassPathResource("log4j.properties");Properties properties = PropertiesLoaderUtils.loadProperties(resource);printProper(properties);}/*** 7.方法七* ResourceBundle.getBundle的路径访问和 Class.getClassLoader.getResourceAsStream类似,* 默认从根目录下读取,也可以读取resources目录下的文件*   ResourceBundle rb = ResourceBundle.getBundle("b")*   不需要指定文件名的后缀,只需要写文件名前缀即可*   可用ResourceBundle.getString(key)取值*/@Testpublic void t7() throws IOException {//读取class根路径的log4j.properties文件,不需要文件后缀ResourceBundle rb2 = ResourceBundle.getBundle("log4j");//可用ResourceBundle.getString(key)取值System.out.println("log4j.appender.A1==="+rb2.getString("log4j.appender.A1"));for(String key : rb2.keySet()){String value = rb2.getString(key);System.out.println(key + ":" + value);}}/*** 获得属性值* @param inputStream* @param key* @return* @throws IOException*/public  static String getVal( InputStream inputStream,String key) throws IOException {Properties properties=new Properties();properties.load(inputStream);return properties.getProperty(key);}public static void printProper(InputStream inputStream)throws IOException {Properties properties=new Properties();properties.load(inputStream);for(Map.Entry<Object, Object> entry: properties.entrySet()){System.out.println(entry.getKey()+"="+entry.getValue());}}public static void printProper(Properties properties)throws IOException {for(Map.Entry<Object, Object> entry: properties.entrySet()){System.out.println(entry.getKey()+"="+entry.getValue());}}
}

4.spring和属性文件

4.1利用注解读取

配置里面注入属性文件

<context:property-placeholder location="classpath:salesman.properties"/><!-- 或者多个 --><bean id="cfgproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>WEB-INF/config/jdbc.properties</value><value>WEB-INF/config/product.properties</value><value>WEB-INF/config/mail.properties</value><value>WEB-INF/config/ding.properties</value></list></property><qualifier value="main"/></bean>
<!-- 属性文件读取 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="properties" ref="cfgproperties" /><property name="fileEncoding" value="utf-8" /></bean>

在Java中使用这个@Value(“${ }”)注解 读取 properties中的参数

	@Value("${filePath}") private String filePath;public void setFilePath(String filePath) {System.out.println(filePath);this.filePath = filePath;
}

4.2配置文件里直接引用

直接用${key}取值

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClassName}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="autoCommitOnClose" value="true"/><property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/><property name="initialPoolSize" value="${cpool.minPoolSize}"/><property name="minPoolSize" value="${cpool.minPoolSize}"/><property name="maxPoolSize" value="${cpool.maxPoolSize}"/><property name="maxIdleTime" value="${cpool.maxIdleTime}"/><property name="acquireIncrement" value="${cpool.acquireIncrement}"/><property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/></bean>

4.属性文件写入

写入属性文件不常用

// 代码示例:写入Properties文件
Properties prop = new Properties(); // 创建Properties对象
OutputStream out = new FileOutputStream("config.properties"); // 创建输出流
prop.setProperty("name", "Tom"); // 设置属性名和属性值
prop.setProperty("age", "22");
prop.store(out, "Properties example"); // 将Properties对象写入Properties文件

5.注意事项

1、Properties文件的优点:

(1)Properties文件结构简单,易于读写、解析。

(2)Properties文件的扩展性强,可以随时添加、修改、删除属性。

(3)Properties文件可以存储键值对类型的数据,非常通用。

(4)Properties文件具有跨平台性,可以在不同操作系统上使用。

2、Properties文件的缺点:

(1)Properties文件对复杂结构和大量数据的支持不够好。

(2)Properties文件格式和内容没有规范,容易产生格式错误、解析异常等问题。

3、注意事项:

(1)Properties文件不是加密文件,存储敏感信息时建议进行加密处理。

(2)Properties文件的编码一般使用ISO-8859-1,建议不要使用Unicode编码。

5.总结

Properties文件是Java中一种存储配置文件的文件类型,其通过键值对的形式存储数据,通常用于存储应用程序的配置信息、国际化内容、资源文件等。在Java中, Properties文件可以通过java.util.Properties类进行读写操作。Properties文件结构简单,易于读写、解析,扩展性强,并且具有跨平台性。但它也有局限性,对于复杂结构和大量数据的支持不够好,容易产生格式错误、解析异常等问题。

http://www.lryc.cn/news/256949.html

相关文章:

  • 如何利用人工智能+物联网技术实现自动化设备生产
  • STM32CubeMx+MATLAB Simulink串口输出实验
  • React中每次渲染都会传入一个新的props.children到子组件?
  • Qt 通过命令行编译程序
  • WireShark监控浏览器登录过程网络请求
  • 202301209将RK3399的挖掘机开发板在Android10下设置系统默认为24小时制
  • 智能优化算法应用:基于法医调查算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 使用MfgTool烧写工具烧写自制系统
  • react中使用react-konva实现画板框选内容
  • es6 相关面试总结
  • 【Hive】——数据仓库
  • 算法基础九
  • QT-在ui界面中给QWidget增加Layout布局的两种方法
  • 免费的网页数据抓取工具有哪些?【2024附下载链接】
  • 报错:Parsed mapper file: ‘file mapper.xml 导致无法启动
  • Linux驱动开发学习笔记2《LED驱动开发试验》
  • hive数据库查看参数/hive查看当前环境配置
  • ajax中get和post的区别,datatype返回的数据类型有哪些?web开发中数据提交的几种方式,有什么区别。百度使用哪种方式?
  • STM32用flash保存参数实现平衡擦写的一种方法
  • Aho Corasick Algorithm
  • 用户管理 --汇总
  • Flutter视频播放器在iOS端和Android端都能实现全屏播放
  • 面试遇到的一些问题(二)
  • JDK8新特性:Lambda表达式规则及用法,方法引用
  • 【GIS】JDK版本升级到17后,GeoServer的图层无法通过openLayer预览
  • vue 批量下载文件,不走后端接口的方法
  • 科技云报道:AI+PaaS,中国云计算市场迎来新“变量”?
  • Windows Service Name重复问题
  • BBS项目
  • Java基础——对象类型转换(向上、向下转型)