- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
**摘要:**在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法。
本文分享自华为云社区《【Java】读取/加载 properties配置文件的几种方法》,作者:Copy工程师。
在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法
我是使用的maven搭建的项目,resources其实就是在根目录下
配置文件很简单
注意:有局限性只能在类路径下比较方便
Properties properties = new Properties();
// 注意这里的路径是根据根目录写的
InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("conf/demo.properties");
properties.load(in);
System.out.println("1111111111111---->:"+properties.getProperty("name"));
输出:
1111111111111---->:xing
Properties properties2 = new Properties();
// 以下两种获取文件流的方式都可以,对于小文件第一种更快一点
// 通过BufferedReader获取文件流
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
properties2.load(bufferedReader);
System.out.println("22222222222---->:"+properties2.getProperty("name"));
}catch (Exception e){
e.printStackTrace();
}
// 通过FileInputStreamm获取文件流
InputStream in2 = new FileInputStream(new File(filePath));
properties2.load(in2);
System.out.println("22222222222---->:"+properties2.getProperty("name"));
输出:
22222222222---->:xing
22222222222---->:xing
// 1. 通过ResourceBundle.getBundle() 静态方法来获取文件 这种方式不需要添加后缀名
// 注意这里的ResourceBundle.getBundle("conf/demo") 这里不需要写配置文件的后缀 只需要名字即可 xml没试过 这里是properties
ResourceBundle resourceBundle = ResourceBundle.getBundle("conf/demo");
System.out.println("333333333333----->:"+resourceBundle.getString("name"));
// 2. 通过InputStream读取文件
InputStream in3 = new FileInputStream(new File(filePath));
ResourceBundle resourceBundle2 = new PropertyResourceBundle(in3);
System.out.println("333333333333----->:"+resourceBundle2.getString("name"));
输出:
333333333333----->:xing
333333333333----->:xing
这是我推荐使用的方法,毕竟有大腿在,干嘛不去抱大腿,嘿嘿。我使用的包是commons-configuration2.x 。记住这是版本2 ,不是版本1, 2和1有很大的差别的。
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-configuration2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.2</version>
</dependency>
try {
// 直接通过路径读取文件
Configurations configurations = new Configurations();
FileBasedConfigurationBuilder.setDefaultEncoding(PropertiesConfiguration.class, "UTF-8");
PropertiesConfiguration propertiesConfiguration = configurations.properties(filePath);
System.out.println("444444444444----->:"+propertiesConfiguration.getString("name"));
//通过reader 读取文件 找了找好像没有通过InputStream读取文件的方式
PropertiesConfiguration propertiesConfiguration1 = new PropertiesConfiguration();
propertiesConfiguration1.read(new BufferedReader(new FileReader(new File(filePath))));
System.out.println("555555555555----->:"+propertiesConfiguration1.getString("name"));
} catch (org.apache.commons.configuration2.ex.ConfigurationException e) {
e.printStackTrace();
}
输出:
444444444444----->:xing
555555555555----->:xing
整个类:
package com.xing.test;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import java.io.*;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class ReadProperties {
public static void main(String[] args) throws IOException {
String filePath = ReadProperties.class.getClassLoader().getResource("conf/demo.properties").getPath();
/** 方法一
* 基于ClassLoader读取配置文件
* 有局限性 只能在类路径下比较方便
*/
Properties properties = new Properties();
// 注意这里的路径是根据target/classes 的路径写的
InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("conf/demo.properties");
properties.load(in);
System.out.println("1111111111111---->:"+properties.getProperty("name"));
/** 方法二
* 基于InputStream读取配置文件
*
*/
Properties properties2 = new Properties();
// 两种获取文件流的方式都可以,对于小文件第一种更快一点
// 通过BufferedReader获取文件流
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
properties2.load(bufferedReader);
System.out.println("22222222222---->:"+properties2.getProperty("name"));
}catch (Exception e){
e.printStackTrace();
}
// 通过FileInputStreamm获取文件流
InputStream in2 = new FileInputStream(new File(filePath));
properties2.load(in2);
System.out.println("22222222222---->:"+properties2.getProperty("name"));
/** 方法三
* 基于ResourceBundle读取配置文件
*
*/
// 1. 通过ResourceBundle.getBundle() 静态方法来获取文件 这种方式不需要添加后缀名
ResourceBundle resourceBundle = ResourceBundle.getBundle("conf/demo");
System.out.println("333333333333----->:"+resourceBundle.getString("name"));
// 2. 通过InputStream读取文件
InputStream in3 = new FileInputStream(new File(filePath));
ResourceBundle resourceBundle2 = new PropertyResourceBundle(in3);
System.out.println("333333333333----->:"+resourceBundle2.getString("name"));
/** 方法四
* 基于PropertiesConfiguration读取配置文件 需要使用第三方的包
*
*/
try {
Configurations configurations = new Configurations();
FileBasedConfigurationBuilder.setDefaultEncoding(PropertiesConfiguration.class, "UTF-8");
PropertiesConfiguration propertiesConfiguration = configurations.properties(filePath);
System.out.println("444444444444----->:"+propertiesConfiguration.getString("name"));
//InputStream in4 = new FileInputStream(new File(filePath));
PropertiesConfiguration propertiesConfiguration1 = new PropertiesConfiguration();
propertiesConfiguration1.read(new BufferedReader(new FileReader(new File(filePath))));
System.out.println("555555555555----->:"+propertiesConfiguration1.getString("name"));
} catch (org.apache.commons.configuration2.ex.ConfigurationException e) {
e.printStackTrace();
}
}
}
通常在读取配置文件的时候都是写个工具类,在程序运行的时候就加载配置文件了。这里简单写了一个,凑合着看吧。
package com.xing.test;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class PropertiesUtils {
private static final Log log = LogFactory.getLog(PropertiesUtils.class);
private static Configurations configurations = null;
private static PropertiesConfiguration propertiesConfiguration = null;
private static void initProperties(){
configurations = new Configurations();
FileBasedConfigurationBuilder.setDefaultEncoding(PropertiesConfiguration.class, "UTF-8");
try {
propertiesConfiguration = configurations.properties(PropertiesUtils.class.getClassLoader().getResource("conf/demo.properties"));
} catch (ConfigurationException e) {
log.error("配置文件初始化失败",e);
}
}
static {
initProperties();
}
/**
* 获取String类型的value
* @param key
* @return
*/
public static String getValueString(String key){
if (propertiesConfiguration == null){
initProperties();
}
return propertiesConfiguration.getString(key);
}
/**
* 获取int类型的value
* @param key
* @return
*/
public static int getValueInt(String key){
if (propertiesConfiguration == null){
initProperties();
}
return propertiesConfiguration.getInt(key, 0);
}
}
其实这个包还可以自动重载的功能,修改了配置文件不需要重启服务器即可加载重载配置文件。
我想要显示正在加载的 .gif,直到所有内容都已加载,包括嵌入的 iframe。但是,目前加载 gif 会在除 iframe 之外的所有内容都已加载后消失。我怎样才能让它等到 iframe 也加载完毕
首先,这是我第一次接触 Angular。 我想要实现的是,我有一个通知列表,我必须以某种方式限制 limitTo,因此元素被限制为三个,在我单击按钮后,其余的应该加载。 我不明白该怎么做: 设置“ V
我正在尝试在我的设备上运行这个非常简单的应用程序(使用 map API V2),并且出于某种原因尝试使用 MapView 时: 使用 java 文件: public class MainMap e
我正在使用 Python 2.6、Excel 2007 Professional 和最新版本的 PyXLL。在 PyXLL 中加载具有 import scipy 抛出异常,模块未加载。有没有人能够在
我想做这个: 创建并打包原始游戏。然后我想根据原始游戏中的蓝图创建具有新网格/声音/动画和蓝图的其他 PAK 文件。原始游戏不应该知道有关其他网格/动画/等的任何信息。因此,我需要在原始游戏中使用 A
**摘要:**在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法。 本文分享自华为云社区《【Java】读取/加载 properties配置文件的几种方法》,作者:Copy工程师。
在 Groovy 脚本中是否可以执行条件导入语句? if (test){ import this.package.class } else { import that.package.
我正在使用 NVidia 视觉分析器(来自 CUDA 5.0 beta 版本的基于 eclipse 的版本)和 Fermi 板,我不了解其中两个性能指标: 全局加载/存储效率表示实际内存事务数与请求事
有没有办法在通过 routeProvider 加载特定 View 时清除 Angular JS 存储的历史记录? ? 我正在使用 Angular 创建一个公共(public)安装,并且历史会积累很多,
使用 Xcode 4.2,在我的应用程序中, View 加载由 segue 事件触发。 在 View Controller 中首先调用什么方法? -(void) viewWillAppear:(BOO
我在某些Django模型中使用JSONField,并希望将此数据从Oracle迁移到Postgres。 到目前为止,当使用Django的dumpdata和loaddata命令时,我仍然没有运气来保持J
创建 Nib 时,我需要创建两种类型:WindowNib 或 ViewNib。我看到的区别是,窗口 Nib 有一个窗口和一个 View 。 如何将 View Nib 加载到另一个窗口中?我是否必须创建
我想将多个env.variables转换为静态结构。 我可以手动进行: Env { is_development: env::var("IS_DEVELOPMENT")
正如我从一个测试用例中看到的:https://godbolt.org/z/K477q1 生成的程序集加载/存储原子松弛与普通变量相同:ldr 和 str 那么,宽松的原子变量和普通变量之间有什么区别吗
我有一个重定向到外部网站的按钮/链接,但是外部网站需要一些时间来加载。所以我想添加一个加载屏幕,以便外部页面在显示之前完全加载。我无法控制外部网站,并且外部网站具有同源策略,因此我无法在 iFrame
我正在尝试为我的应用程序开发一个Dockerfile,该文件在初始化后加载大量环境变量。不知何故,当我稍后执行以下命令时,这些变量是不可用的: docker exec -it container_na
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我刚刚遇到一个问题,我有一个带有一些不同选项的选择标签。 现在我想检查用户选择了哪些选项。 然后我想将一个新的 html 文件加载到该网站(取决于用户选中的选项)宽度 javascript,我该怎么做
我知道两种保存/加载应用程序设置的方法: 使用PersistentStore 使用文件系统(存储,因为 SDCard 是可选的) 我想知道您使用应用程序设置的做法是什么? 使用 PersistentS
我开始使用 Vulkan 时偶然发现了我的第一个问题。尝试创建调试报告回调时(验证层和调试扩展在我的英特尔 hd vulkan 驱动程序上可用,至少它是这么说的),它没有告诉我 vkCreateDeb
我是一名优秀的程序员,十分优秀!