一、通过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. 分享一下我封装iOS自定义控件的体会,附上三个好用的控件Demo <时间选择器&多行输入框&日期选择器>

    前段时间有小伙伴问到我:"这样的控件该怎么做呢?",我感觉是个比较简单的控件,可能对于入行不久的同志思路没有很清晰吧.趁着最近工作不忙,就来这里分享一下我封装自定义控件的几点体会吧 ...

  2. mac ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    好久不用mysql,今天突然想用的时候, mysql -uroot -p 直接报了下面的错误 ERROR 2002 (HY000): Can't connect to local MySQL serv ...

  3. C#方法的重载和方法的可变参数

    方法的重载 1.方法重载的前提:方法名称必须一样 2.构成重载的条件:参数不一样(参数数量不一样,参数类型不一样) 方法的可变参数 1.可变参数的值的数量可以是0到多个. 2.可变参数调用的时候,没有 ...

  4. [CLR via C#]10. 属性

    一.无参属性 对于字段,强烈建议将所有的字段都设为private.如果允许用户或类型获取或设置状态信息,就公开一个针对该用途的方法.封装了字段访问的方法通常称为访问器(accessor)方法.访问器方 ...

  5. c#调用word com组件 替换书签套打

    安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...

  6. [转载]Ubuntu14.04 LTS更新源

    不同的网络状况连接以下源的速度不同, 建议在添加前手动验证以下源的连接速度(ping下就行),选择最快的源可以节省大批下载时间. 首先备份源列表: sudo cp /etc/apt/sources.l ...

  7. python 的import机制2

    http://blog.csdn.net/sirodeng/article/details/17095591   python 的import机制,以备忘: python中,每个py文件被称之为模块, ...

  8. [PHP] PHP请求Socket接口测试

    使用php读取socket接口的数据,通过php传递请求方法和请求参数,得到返回结果 PHP文件: <?php class Test{ const IP='127.0.0.1'; const p ...

  9. Mysql的简单使用(三)

    接上文Mysql的简单使用(二) mysql中结构相同的两个表进行合并:(注意需要两个表的结构是一样的) 有如下结构的两个表father和person. 合并的步骤为: 1.把person表和fath ...

  10. php学习笔记:对文件的增删查改等操作

    文件的创建: 采用touch()函数,当文件不存在会被创建 例如: <?php header("Content-type: text/html; charset=utf-8" ...