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

做网站公司郑州郑州的网站建设公司哪家好海淀网站建设公司

做网站公司郑州郑州的网站建设公司哪家好,海淀网站建设公司,阿里企业邮箱个人版,wordpress+新打开空白如何配置和读取属性文件 1.属性文件介绍1.1 什么是属性文件1.2属性文件规范1.3 属性文件优缺点 2.属性文件读取4.spring和属性文件4.1利用注解读取4.2配置文件里直接引用 4.属性文件写入5.注意事项5.总结 1.属性文件介绍 1.1 什么是属性文件 Java开发中,我们经常需…

如何配置和读取属性文件

  • 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.hkea.cn/news/879251/

相关文章:

  • 人是用什么做的视频网站网络营销方案设计毕业设计
  • 建设网站考虑因素关键词优化是怎么弄的
  • 陕西营销型网站建设推广普通话的内容简短
  • 做配电箱的专门网站百度指数属于行业趋势及人群
  • 学做网站的网站重庆seo整站优化报价
  • 保定网站设计概述seo推广软件排名
  • 查pv uv的网站网络营销推广服务
  • 怎样让客户做网站优化 保证排名
  • 企业营销型网站做的好网络营销的有哪些特点
  • 网站开发 合同兰州快速seo整站优化招商
  • 网站开发技术现状深圳网络营销推广培训
  • 知名网络公司有哪些河北网站seo
  • 学做网站多少钱关键词难易度分析
  • 传奇如何做网站网站建设策划书案例
  • 龙岗 网站建设深圳信科最好用的搜索神器
  • 动态网站开发日志重庆seo整站优化报价
  • 魔站网站建设微信公众号运营推广方案
  • 好的网站建设公司营销推广外包公司
  • 教育机构做网站素材长尾关键词爱站
  • 做网站选什么系统企业网站seo推广
  • 山东省南水北调建设管理局网站腾讯网qq网站
  • 菏泽做网站公司sem网络营销
  • 专业建站外包兰州网络优化seo
  • 企业邮箱腾讯杭州seo按天计费
  • 政府网站建设先进个人事迹互动营销
  • 网站建设之织梦模板做国外网站
  • 小程序电商模板seo关键词排名优化品牌
  • 泉州网站优化排名百度关键字优化价格
  • 上海网站建设好处win优化大师官网
  • 适合毕设做的简单网站初学seo网站推广需要怎么做