java代码如何读取properties文件
我们在开发工程中,有时候需要在Java代码中定义一些在部署生产环境时容易改变的变量,还需要我们单独放在一个外部属性文件中,方便我们将来修改。这里列出了两种比较方便的方式。
一、在Spring配置文件中使用 util:properties 标签进行暴露 properties 文件中的内容,需要注意的是需要在Spring的配置文件的头部声明以下部分。
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
1. 在classpath路径下新建 config.properties 内容如下:
app.id=appId
2. 修改Spring的配置文件
<util:properties id="appConfig" location="classpath:config.properties"></util:properties>
3. 在需要调用的类中声明全局变量,加上@Value注解
@Value("#{appConfig['app.id']}")
private String appId;
二、使用Java的Properties类
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; /**
* properties文件获取工具类
*/
public class PropertyUtil { private static Properties props;
static {
loadProps();
} synchronized static private void loadProps(){
System.out.println("开始加载properties文件内容.......");
props = new Properties();
InputStream in = null;
try {
// 第一种,通过类加载器进行获取properties文件流-->
in = PropertyUtil.class.getClassLoader().getResourceAsStream("db.properties");
// 第二种,通过类进行获取properties文件流-->
//in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
props.load(in);
} catch (FileNotFoundException e) {
System.out.println("jdbc.properties文件未找到");
} catch (IOException e) {
System.out.println("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
System.out.println("jdbc.properties文件流关闭出现异常");
}
}
System.out.println("加载properties文件内容完成...........");
System.out.println("properties文件内容:" + props);
} /**
* 根据key获取配置文件中的属性
*/
public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
} /**
* 根据key获取配置文件中的属性,当为null时返回指定的默认值
*/
public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}
测试:
String appId = PropertyUtil.getProperty("app.id");
System.out.println("appId = " + appId);
java代码如何读取properties文件的更多相关文章
- Java项目中读取properties文件,以及六种获取路径的方法
下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...
- 分别用Java和JS读取Properties文件内容
项目中经常用到的配置文件,除了XML文件之外,还会用到Properties文件来存储一些信息,例如国际化的设置.jdbc连接信息的配置等.有时候也会把一些路径或者sql语句放到Properties中, ...
- WEB应用中普通java代码如何读取资源文件
首先: 资源文件分两种:后缀.xml文件和.properties文件 .xml文件:当数据之间有联系时用.xml .properties文件:当数据之间没有联系时用.properties 正题: ...
- java web中读取properties文件时的路径问题
在web开发时,难免会有一些固定的参数,我们一般把这些固定的参数存在properties文件中,然后用的时候要读出来.但经常出现一些错误,找不到相应的路径,所以,今天特地讲一些如何正确获得路径. 首先 ...
- 格式化MyEclipse代码(java、jsp、js)行的长度@修改java代码字体@修改Properties文件编码方式
每次用MyEclipse/Eclipse自带的快捷键Ctrl+shift+f格式化代码时,如果原来的一行代码大于80列,Eclipse就会自动换为多行.如果想格式化代码后不想让代码换行可以通过以下方式 ...
- Spring用代码来读取properties文件
我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyP ...
- java项目中读取properties文件
这里的配置文件都放在src下面, System.properties的内容 exceptionMapping=exceptionMapping.properties config=config.pro ...
- (转)Spring用代码来读取properties文件
转至http://www.cnblogs.com/Gyoung/p/5507063.html 我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org ...
- classpath 及读取 properties 文件
java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...
随机推荐
- NYOJ-1057 寻找最大数(三)(贪心)
寻找最大数(三) 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数. 求这个新的整数的 ...
- Python笔记6(异常)-20160924
1. NameError 当视图访问一个未定义的变量则会发生NameError.
- JavaScript-Curry
Currying allows you to easily create custom functions by partially invoking an existing function. He ...
- Windows Server2012上使用Nginx做文件服务器
由于项目中用到了大量的文件上传和删除,考虑到安全的因素,所以整体的思路是使用FTP从主服务器把文件资源上传到文件服务器上. FTP上传到服务器的代码如下(简单附加一下,具体的网上很多) public ...
- MyEclipse中用Maven创建Web项目
方法/步骤 new --> other 1.Wizards: mvaen 2.Maven Project 3.Next Use Default Workspace Locatio ...
- Hibernate 异常 集锦
异常1.Error parsing JNDI name [foo] 异常信息摘要: org.hibernate.engine.jndi.JndiException: Error parsing JND ...
- freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建
今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...
- linux重要目录说明
1 home :root用户的home 目录是root,普通用户的home 目录是/home,users/(不要随便使用root权限,小白容易改错东西哒) 2 bin:常用可执行文件:/bin./us ...
- sqlserver2012评估期已过问题处理
于之前安装sqlserver2012忘记输入序列号,现在出现评估期已过的问题,网上忙活半天,才解决,发现网上叙述都很凌乱,而且只有大意,新手很难操作,所以把我操作的过程分享给大家 步骤阅读 百度经 ...
- 在js中拼接<a>标签,<a>标签中含有onclick事件,点击无法触发该事件
我们在<a>标签中添加事件一般是onclick="editUser()" 这样添加,在html页面上是行的通的 但是如何你是在js中拼接<a>标签并在< ...