一、通过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资源文件的更多相关文章

  1. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  2. 在服务端中,读取properties资源文件中的数据

    1.获取到资源的路径 2.读取数据 //properties文件对象 Properties properties = new Properties(); //通过HttpServletRequest ...

  3. Java项目读取resources资源文件路径那点事

    今天在Java程序中读取resources资源下的文件,由于对Java结构了解不透彻,遇到很多坑.正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径, ...

  4. Java中读取.properties配置文件的通用类

    由于Java中读取配置文件的代码比较固定,所以可以将读取配置文件的那部分功能单独作为一个类,以后可以复用.为了能够达到复用的目的,不能由配置文件中每一个属性生成一个函数去读取,我们需要一种通用的方法读 ...

  5. Java中读取 .properties 和 .xml 文件

    配置文件内容获取 总结内容 1. Java中为什么要使用配置文件 2. Java中常用的配置文件类型有哪些以及它们的特点 Properties配置文件 XML配置文件 总结 总结内容 1. Java中 ...

  6. Spring读取properties资源文件

    我们知道可以通过读取资源文件流后加载到Properties对象,再使用该对象方法来获取资源文件.现在介绍下利用Spring内置对象来读取资源文件. 系统启动时加载资源文件链路:web.xml --&g ...

  7. 2018-10-10 在浏览器插件中读取JSON资源文件

    续前文: 浏览器插件实现GitHub代码翻译原型演示 此改进只为演示: 词典数据提取到json文件 · program-in-chinese/webextension_github_code_tran ...

  8. 六种方式读取properties资源文件

    conf.properties文件内容: reportStationName=xx供电局 JBM=0318 文件路径: 其中xxx为项目名 import java.io.BufferedInputSt ...

  9. spring mvc读取properties资源文件夹中文乱码问题

    通过在applicationContext.xml和springmvc.xml中配置 <bean        class="org.springframework.beans.fac ...

随机推荐

  1. UINavigationItem和UItabBarItem的区别详解

    一.UINavigationItem 1> 获得方式 self.navigationItem // self是指控制器 2> 作用 可以用来设置当前控制器顶部导航栏的内容 // 设置导航栏 ...

  2. Scrum 项目 7.0

    ------------------7.0------------------------------ Sprint回顾 1.回顾组织 主题:“我们怎样才能在下个sprint中做的更好?” 时间:设定 ...

  3. IOS 之 PJSIP 笔记(一) 编译多平台支持的静态库

    好久没有写博客了,这也算是我步入新工作后的第一篇技术博文吧.在进入新公司前,早就有了技术层进入下一个迭代的准备,但很多事情是意想不到的,就像我以 C# 程序员的身份面试入职的,而今却是一个全职的 IO ...

  4. CSS浏览器兼容性写法小结

    *        , ie6,ie7可以识别: _和- ,  ie6可以识别: !important  ,表示高优先级,ie7及以上,firefox都支持,ie6认识带!important的样式属性, ...

  5. 【循序渐进学Python】4. Python中的序列——字典

    字典是Python内建的六种序列之一.字典作为一种常用的数据结构,字典中的值没有特定顺序,每个值都对应于一个唯一的键.键可以是数字.字符串甚至是元组. 1. 创建和使用字典 Python中字典可以使用 ...

  6. java内部类的使用

    内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...

  7. 动态生成RDLC报表

    前段时间,做了RDLC报表,主要是三块功能: 1.从DataGrid提取(包括最新的增删改)的数据,自动生成对应的RDLC报表文件(以流的形式驻存在内存中),用ReportViewer类来展示.打印. ...

  8. csharp: Export DataSet into Excel and import all the Excel sheets to DataSet

    /// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...

  9. strtr对用户输入的敏感词汇进行过滤

    /** * 过滤用户输入的基本数据,防止script攻击 * * @access public * @return string */ function compile_str($str) { $ar ...

  10. fibonacci数列的和取余(1)

    As we know , the Fibonacci numbers are defined as follows:  """" Given two numbe ...