亿赐客网站怎么样,wap购物网站源码,北辰正方建设集团有限公司官方网站,乐陵森林酒店家具如何配置和读取属性文件 1.属性文件介绍1.1 什么是属性文件1.2属性文件规范1.3 属性文件优缺点 2.属性文件读取4.spring和属性文件4.1利用注解读取4.2配置文件里直接引用 4.属性文件写入5.注意事项5.总结 1.属性文件介绍
1.1 什么是属性文件
Java开发中#xff0c;我们经常需… 如何配置和读取属性文件 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类型的因此我们完全可以用MapString, String来表示它。
但因为配置文件特别常用所以Java集合库给我们提供了一个Properties类来表示一组“配置”专门用来处理keyvalue形式的配置信息。Properties类可以表示一个持久的属性集每个键及其对应的值都是字符串类型它可以把配置信息保存在一个IO流中或是从一个IO流中加载配置信息因此很适合用来处理配置文件。
Properties的内部本质上是一个Hashtable该类从Hashtable中继承了get()和put()方法这些方法的参数签名是Object。但由于历史遗留原因Properties的设计实际上是有问题的不过为了保持兼容性现在已经没法修改了。所以我们在使用Properties时不要去调用这些从Hashtable继承来的方法而应该使用Properties自身关于读写配置的方法比如getProperty()和setProperty()等方法。
1.2属性文件规范 1.属性文件都是以键值对出现 keyvalue 2.注释用#开头 3.增加可读性key很多时候都用xx.xxx表示 eg:
#log4j属性配置
log4j.rootLoggerDEBUG,A
log4j.appender.A1org.apache.log4j.ConsoleAppender
log4j.appender.A1.layoutorg.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern[%t] [%c]-[%p] %m%n# properties文件示例
# 注释内容以#号开头
nameTom
age22
genderMale1.3 属性文件优缺点
1、Properties文件的优点
1Properties文件结构简单易于读写、解析。
2Properties文件的扩展性强可以随时添加、修改、删除属性。
3Properties文件可以存储键值对类型的数据非常通用。
4Properties文件具有跨平台性可以在不同操作系统上使用。
2、Properties文件的缺点
1Properties文件对复杂结构和大量数据的支持不够好。
2Properties文件格式和内容没有规范容易产生格式错误、解析异常等问题。
3、注意事项
1Properties文件不是加密文件存储敏感信息时建议进行加密处理。
2Properties文件的编码一般使用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);等价ClassLoaderResourceBundle 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 inputStreamnull;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 inputStreamnull;//放在在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 inputStreamnull;//放在在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 inputStreamnull;inputStreamnew FileInputStream(src/main/resources/log4j.properties);inputStreamnew FileInputStream(target/classes/log4j.properties);inputStreamnew 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 propertiesnew 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.A1rb2.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 propertiesnew Properties();properties.load(inputStream);return properties.getProperty(key);}public static void printProper(InputStream inputStream)throws IOException {Properties propertiesnew Properties();properties.load(inputStream);for(Map.EntryObject, Object entry: properties.entrySet()){System.out.println(entry.getKey()entry.getValue());}}public static void printProper(Properties properties)throws IOException {for(Map.EntryObject, Object entry: properties.entrySet()){System.out.println(entry.getKey()entry.getValue());}}
}4.spring和属性文件
4.1利用注解读取 配置里面注入属性文件 context:property-placeholder locationclasspath:salesman.properties/!-- 或者多个 --bean idcfgproperties classorg.springframework.beans.factory.config.PropertiesFactoryBeanproperty namelocationslistvalueWEB-INF/config/jdbc.properties/valuevalueWEB-INF/config/product.properties/valuevalueWEB-INF/config/mail.properties/valuevalueWEB-INF/config/ding.properties/value/list/propertyqualifier valuemain//bean
!-- 属性文件读取 --bean idpropertyConfigurer classorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty nameproperties refcfgproperties /property namefileEncoding valueutf-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 iddataSource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${jdbc.driverClassName} /property namejdbcUrl value${jdbc.url} /property nameuser value${jdbc.username} /property namepassword value${jdbc.password} /property nameautoCommitOnClose valuetrue/property namecheckoutTimeout value${cpool.checkoutTimeout}/property nameinitialPoolSize value${cpool.minPoolSize}/property nameminPoolSize value${cpool.minPoolSize}/property namemaxPoolSize value${cpool.maxPoolSize}/property namemaxIdleTime value${cpool.maxIdleTime}/property nameacquireIncrement value${cpool.acquireIncrement}/property namemaxIdleTimeExcessConnections value${cpool.maxIdleTimeExcessConnections}//bean4.属性文件写入
写入属性文件不常用
// 代码示例写入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文件的优点
1Properties文件结构简单易于读写、解析。
2Properties文件的扩展性强可以随时添加、修改、删除属性。
3Properties文件可以存储键值对类型的数据非常通用。
4Properties文件具有跨平台性可以在不同操作系统上使用。
2、Properties文件的缺点
1Properties文件对复杂结构和大量数据的支持不够好。
2Properties文件格式和内容没有规范容易产生格式错误、解析异常等问题。
3、注意事项
1Properties文件不是加密文件存储敏感信息时建议进行加密处理。
2Properties文件的编码一般使用ISO-8859-1建议不要使用Unicode编码。
5.总结
Properties文件是Java中一种存储配置文件的文件类型其通过键值对的形式存储数据通常用于存储应用程序的配置信息、国际化内容、资源文件等。在Java中 Properties文件可以通过java.util.Properties类进行读写操作。Properties文件结构简单易于读写、解析扩展性强并且具有跨平台性。但它也有局限性对于复杂结构和大量数据的支持不够好容易产生格式错误、解析异常等问题。