Properties读取小结
一、Java程序中读取properties文件
加载的工具类:
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; /**
* Properties工具类
* @author happyqing
* @since 2014.6.6
*/
public class PropertiesUtil {
private static final Log log = LogFactory.getLog(PropertiesUtil.class);
private static Properties env = new Properties(); static {
try {
//PropertiesHelper.class.getResourceAsStream("env.properties"); // /com/cici/conf/env.properties
//ClassLoader.getSystemResourceAsStream("env.properties");
InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream("env.properties");
env.load(is);
is.close();
} catch (Exception e) {
log.error(e);
}
} /**
* 取属性值
* @param key
* @return
*/
public static String getProperty(String key){
return env.getProperty(key);
} /**
* 设置属性值
* @param key
* @param value
*/
public static void setProperty(String key, String value){
try{
File file = new File(PropertiesUtil.class.getClassLoader().getResource(".").getPath()+File.separator+"env.properties");
FileOutputStream outStream = new FileOutputStream(file);
env.setProperty(key, value);
//写入properties文件
env.store(outStream, null);
} catch (Exception ex) {
log.error(ex);
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(PropertiesUtil.getProperty("txtLength"));
//System.out.println(PropertiesUtil.class.getClassLoader().getResource(".").getPath());
}
}
频繁的配置文件读取与操作,推荐apache commons大家庭的成员:commons-configuration
文件结构目录如图所示:

其中,config2为与src同级的sourec folder,c.properties位于src根目录,b.properties位于src/config1 folder下
a.properties位于cn.package1包下。所有结果均已成功测试,测试环境为Myeclipse2016+JDK8
其实以下也就是程序路径的区分
1.读取a.properties:
package cn.package1; import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import org.junit.Test; public class Demo01 {
@Test
public void fun1() throws IOException{
InputStream in1 = Demo01.class.getClassLoader()
.getResourceAsStream("cn/package1/a.properties");
Properties props = new Properties();
props.load(in1);
String value1 = props.getProperty("name");
System.out.println(value1);
}
}
(输入流的处理以及关闭可以改进)
2.读取b.properties:
(重复代码已经省略!)
InputStream in1 = Demo01.class.getClassLoader()
.getResourceAsStream("config1/b.properties");
3.读取c.properties:
InputStream in1 = Demo01.class.getClassLoader()
.getResourceAsStream("c.properties");
4.读取d.properties:
InputStream in1 = Demo01.class.getClassLoader()
.getResourceAsStream("d.properties");
二、Spring项目中读取properties
总之就是,一定要加载到properties文件然后才能读取(xml文件读取或者java代码读取),至于加载的方式,可以是下面的直接使用context标签进行加载,例如使用classpath:*.properties(见下文配置文件),或者使用文末随笔中提到的使用spring的bean来进行加载!
1.配置文件中使用——使用${}取值即可
spring配置文件如何读取属性配置文件:
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:jeesite.properties" />
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
springMVC中进行配置:
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:jeesite.properties" /> <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
<context:component-scan base-package="com.thinkgem.jeesite" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
// 必须注意,不使用默认的过滤器!
2.Java代码中取值——使用@Value读取
/**
* 管理基础路径
*/
@Value("${adminPath}")
protected String adminPath; /**
* 前端基础路径
*/
@Value("${frontPath}")
protected String frontPath; /**
* 前端URL后缀
*/
@Value("${urlSuffix}")
protected String urlSuffix;
// 像这里我们可以直接把它做成一个父类,这样每个类只需要继承父类便可使用此变量,而无须重复使用每个类的局部变量
请谨记很多东西没必要写死的应该写在配置文件中,比如服务器的地址,数据库的密码等,不应该在程序中写死,而应该归配置文件管理!
spring中读取配置文件也可以参见:http://www.cnblogs.com/Gyoung/p/5507063.html
Properties读取小结的更多相关文章
- java properties读取与设值
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...
- 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)
从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...
- Properties读取properties配置文件
package cn.rocker.readProperties; import java.io.IOException; import java.io.InputStream; import jav ...
- ResourceBundle与Properties读取配置文件
ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...
- 解决使用Properties读取中文乱码问题
web服务返回的是多行以key和value对应的键值对,且编码为utf-8.我的项目使用的编码也是utf-8,但是我用Properties读取中文的时候,打印出来的总是乱码. 后来网上查了一下,得到如 ...
- ResourceBundle和properties 读取配置文件区别
java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Properties同时还能用来写文 ...
- java.util.Properties 读取配置文件中的参数
用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...
- 【spring boot】SpringBoot初学(2.1) - properties读取明细
前言 算是对<SpringBoot初学(2) - properties配置和读取>的总结吧. 概念性总结 一.Spring Boot允许外化(externalize)你的配置.可以使用pr ...
- Properties读取属性文件
import java.util.*;import java.io.*;class PropertiesDemo{ public static void main(String[] args) thr ...
随机推荐
- Centos 使用C++11 编译
今天编译代码,发现使用auto后无法编译,我的当前linux内核版本:(4.7之后即可支持C++11) 这时,在编译末尾加入 -std=c++11 就可以正常编译了.如:
- ExpressRoute 线路和路由域
你必须订购一条 ExpressRoute 线路 ,以通过连接提供商将你的本地基础结构连接到 Azure.下图提供了你的 WAN 与 Azure 之间的连接的逻辑表示形式. ExpressRoute 线 ...
- SVN合并时报错:Merge tracking not allowed with missing subtrees; try restoring these items
使用的是TortoiseSVN; Merge tracking not allowed with missing subtrees; try restoring these items 下面会有跟着几 ...
- MySQL存储引擎之Spider内核深度解析
作者介绍 朱阅岸,中国人民大学博士,现供职于腾讯云数据库团队.研究方向主要为数据库系统理论与实现.新硬件平台下的数据库系统以及TP+AP型混合系统. Spider是为MySQL/MariaDB开发 ...
- java中如何打war包
1.利用jdk里的工具 例如我们要打包的文件在D:\Project:运行 cmd: cd D:\Project 进入D:\Project ,然后输入jar -cvf Project.war *回 ...
- 用UITextView模拟UITextField的placeHolder
用UITextView模拟UITextField的placeHolder 效果: 源码: // // ViewController.m // TextView // // Created by You ...
- Replace-iOS
Replace-iOS https://github.com/MartinRGB/Replace-iOS 看了下demo,运行起来超卡...... Simply Implement Zee Young ...
- 审计系统---paramiko模块学习
paramiko模块学习 [更多参考]http://www.cnblogs.com/wupeiqi/articles/4963027.html [paramiko的Demo实例]https://git ...
- Windows Server 2008搭建域控制器
前言 1.为什么要建域 工作组的分散管理模式不适合大型的网络环境下工作,域模式就是针对大型的网络管理需求设计的,就是共享用户账号,计算机账号和安全策略的计算机集合.域中集中存储用户账号的计算机就是域控 ...
- 【原创】python requests 库底层Sockets处于close_wait状态
以前对于Requests库只是简单是使用,在现在公司的后台中,有多个接口是直接使用requests.get .post之类的方法来做的,进行过一段时间的压力测试,发现性能低的可怜,且linux服务器有 ...