Java中读取properties资源文件
一、通过ResourceBundle来读取.properties文件
/**
* 通过java.util.resourceBundle来解析properties文件。
* @param String path:properties文件的路径
* @param String key: 获取对应key的属性
* @return String:返回对应key的属性,失败时候为空。
*/
public static String getPropertyByName1(String path,String key){
String result = null;
try {
result = ResourceBundle.getBundle(path).getString(key).trim();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
对于String path的填写,要注意。一般分为两种情况:
1、.properties文件在src目录下面,文件结构如下所示:
|src/
— —test.properties
2、.properties文件在src目录下面的一个包中,因为可能我们经常习惯把各种properties文件建立在一个包中。文件结构如下:
|src/
|— —configure/
| | — —test1.properties
| | — —test2.properties
对于第一种情况,在main函数中使用方法如下:
System.out.println(GetConfigureInfo.getPropertyByName1("test", "key1"));
对于第二种情况,在main函数中使用方法如下:
System.out.println(GetConfigureInfo.getPropertyByName1("configure.test1", "key1"));
这种用法下,path不用带.properties后缀,直接输入就好了。
二、通过getResourceAsStream方式加载.properties文件
/**
* 解析properties文件。
* @param String path:properties文件的路径
* @param String key: 获取对应key的属性
* @return String:返回对应key的属性,失败时候为null。
*/
public String getPropertyByName2(String path,String key){
String result = null; Properties properties = new Properties();
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
if(inputStream == null){
properties.load(inputStream);
result = properties.getProperty(key).trim();
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
} return result;
}
同样两种情况:
1、.properties文件在src目录下面
使用方法是:
GetConfigureInfo getConfigureInfo = new GetConfigureInfo();
System.out.println(getConfigureInfo.getPropertyByName2("test1.properties", "key1"));
2、.properties文件在src目录下面的一个包中:
使用方法:
GetConfigureInfo getConfigureInfo = new GetConfigureInfo();
System.out.println(getConfigureInfo.getPropertyByName2("configure/test1.properties", "key1"));
可以看到很明显的这种类似于文件的读写,所以用法有所不同了都。
三、使用FileOutputStream和Propertity写入test.properties文件:
/**
* 写入.properties文件, key=content
* @param String path:写入文件路径
* @param String key:写入的key
* @param String content:写入的key的对应内容
* @return void
*/
public void setProperty(String path,String key,String content){
Properties properties = new Properties();
try {
properties.setProperty(key, content);
//true表示追加。
if((new File(path)).exists()){
FileOutputStream fileOutputStream = new FileOutputStream(path, true);
properties.store(fileOutputStream, "just for a test of write");
System.out.println("Write done");
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
使用方式:
GetConfigureInfo getConfigureInfo = new GetConfigureInfo();
getConfigureInfo.setProperty("src/testConfig.properties", "test3", "test3 value");
值得注意的是,现在这个函数里面的path居然加了src/,因为是用FileOutputStream,所以默认的主路径是项目路径。
总结:
对于Java而言,我觉得这些路径就搞得很不合理的样子,现在看来,使用输入输出流读写文件时候,似乎主路径都是在项目下。
而对于ResourceBundle读取properties文件的路径不加.properties也很奇特啊。似乎java中各种不同方式来加载文件时候都有默认的主路径(也可以说成根目录)
根目录也就决定了这个路径到底应该怎么写,才能被程序识别。我现在还不得而知,先记录下这些区别。
如果大家有什么想指正、教育我的地方,欢迎指出,小弟想知道到底是怎么样子的。
Java中读取properties资源文件的更多相关文章
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- 在服务端中,读取properties资源文件中的数据
1.获取到资源的路径 2.读取数据 //properties文件对象 Properties properties = new Properties(); //通过HttpServletRequest ...
- Java项目读取resources资源文件路径那点事
今天在Java程序中读取resources资源下的文件,由于对Java结构了解不透彻,遇到很多坑.正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径, ...
- Java中读取.properties配置文件的通用类
由于Java中读取配置文件的代码比较固定,所以可以将读取配置文件的那部分功能单独作为一个类,以后可以复用.为了能够达到复用的目的,不能由配置文件中每一个属性生成一个函数去读取,我们需要一种通用的方法读 ...
- Java中读取 .properties 和 .xml 文件
配置文件内容获取 总结内容 1. Java中为什么要使用配置文件 2. Java中常用的配置文件类型有哪些以及它们的特点 Properties配置文件 XML配置文件 总结 总结内容 1. Java中 ...
- Spring读取properties资源文件
我们知道可以通过读取资源文件流后加载到Properties对象,再使用该对象方法来获取资源文件.现在介绍下利用Spring内置对象来读取资源文件. 系统启动时加载资源文件链路:web.xml --&g ...
- 2018-10-10 在浏览器插件中读取JSON资源文件
续前文: 浏览器插件实现GitHub代码翻译原型演示 此改进只为演示: 词典数据提取到json文件 · program-in-chinese/webextension_github_code_tran ...
- 六种方式读取properties资源文件
conf.properties文件内容: reportStationName=xx供电局 JBM=0318 文件路径: 其中xxx为项目名 import java.io.BufferedInputSt ...
- spring mvc读取properties资源文件夹中文乱码问题
通过在applicationContext.xml和springmvc.xml中配置 <bean class="org.springframework.beans.fac ...
随机推荐
- spring AspectJ的Execution表达式
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execu ...
- php的SAPI,CLI SAPI,CGI SAPI
首先一个问题:在命令行下执行:php -r 'echo 12;' 控制台会打印出 12: 这个过程不是很奇妙么,我输入的是shell命令,但是执行的却是php脚本.php脚本执行完成之后的输出还能在控 ...
- JS魔法堂:doctype我们应该了解的基础知识
一.前言 什么是doctype?其实我们一直使用,却很少停下来看清楚它到底是什么,对网页有什么作用.本篇将和大家一起探讨那个默默无闻的doctype吧! 二.什么是doctype doctype或DT ...
- Excel中显示长数字的方法
主要有以下三种方法: 1.先设置为文本格式,再粘贴2.在另一列输入=CONCATENATE(A1),双击此格右下角得到全部数值,再格式化为文本粘贴回去3.选中数据列,点数据-分列,下一步-下一步,选中 ...
- SQL增删查改注意的事项
一.新增 1.增加的时候,bit字段要用“0,1”表示false,和true: 2.时间字段,用单引号包括,里面要遵循基本时间格式: 3,不能为标识列(自动编号列)插入数据(特殊情况下:set ide ...
- Requested registry access is not allowed(不允许所请求的注册表访问权)
尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息 EventLog.CreateEventS ...
- Tesseract-OCR引擎 入门
OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程. Tesseract:开源的OCR识别引擎,初期Tesseract引 ...
- ASP.NET几种页面数据绑定的用法及区别: <%#、 <%=、 <% 、<%@
< %#... %>: 是在绑定控件DataBind()方法执行时被执行,用于数据绑定 如: < %# Container.DataItem("tit") %&g ...
- 内存分段 && 缓冲区 && 析构函数
一.内存中的程序: 在进程被载入内存中时,基本上被分成许多小的节,以下是6个主要的节. 低地址 高地 ...
- java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect
java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect 2014年06月29日 ...