注意:

  properties文件要放在classPath下面,也就是与src下。

path.properties(在properties文件中\代表着没有完,下行同本行是一个内容)

perfectMaterial=fileLibrary/completeFile/perfectMaterial
perfectMaterialPDF=fileLibrary/completeFile/perfectMaterialPDF
promiseFile=fileLibrary/originalFile/promiseFile
signatureFile=signature
signatureBackupFile=signatureBackup
investigatePicture=fileLibrary/originalFile/investigatePicture
auditPicture=fileLibrary/originalFile/auditPicture
auditReprt=fileLibrary/originalFile/auditReprt
sevenMaterial=fileLibrary/originalFile/sevenMaterial
sevenMaterialPdf=fileLibrary/originalFile/PDFFile/sevenMaterialPdf
auditReportWordAndPdf=fileLibrary/originalFile/auditReportWordAndPdf
jdbc.test=fileLibrary/originalFile/auditReportWordAndPdf hello=hell\
onimei

 ResourcesUtil.java

package Utils.ResourceUtil;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set; /**
* 资源文件读取工具类
*
*/
public class ResourcesUtil implements Serializable { private static final long serialVersionUID = -7657898714983901418L; /**
* 系统语言环境,默认为中文zh
*/
public static final String LANGUAGE = "zh"; /**
* 系统国家环境,默认为中国CN
*/
public static final String COUNTRY = "CN";
private static Locale getLocale() {
Locale locale = new Locale(LANGUAGE, COUNTRY);
return locale;
} /**
* 根据语言、国家、资源文件名和key名字获取资源文件值
*
* @param language
* 语言
*
* @param country
* 国家
*
* @param baseName
* 资源文件名
*
* @param section
* key名字
*
* @return 值
*/
private static String getProperties(String baseName, String section) {
String retValue = "";
try {
Locale locale = getLocale();
ResourceBundle rb = ResourceBundle.getBundle(baseName, locale);
retValue = (String) rb.getObject(section);
} catch (Exception e) {
e.printStackTrace();
// TODO 添加处理
}
return retValue;
} /**
* 通过key从资源文件读取内容
*
* @param fileName
* 资源文件名
*
* @param key
* 索引
*
* @return 索引对应的内容
*/
public static String getValue(String fileName, String key) {
String value = getProperties(fileName,key);
return value;
} public static List<String> gekeyList(String baseName) {
Locale locale = getLocale();
ResourceBundle rb = ResourceBundle.getBundle(baseName, locale); List<String> reslist = new ArrayList<String>(); Set<String> keyset = rb.keySet();
for (Iterator<String> it = keyset.iterator(); it.hasNext();) {
String lkey = (String)it.next();
reslist.add(lkey);
} return reslist; } /**
* 通过key从资源文件读取内容,并格式化
*
* @param fileName
* 资源文件名
*
* @param key
* 索引
*
* @param objs
* 格式化参数
*
* @return 格式化后的内容
*/
public static String getValue(String fileName, String key, Object[] objs) {
String pattern = getValue(fileName, key);
String value = MessageFormat.format(pattern, objs);
return value;
} public static void main(String[] args) {
System.out.println(getValue("resources.messages", "101",new Object[]{100,200})); //根据操作系统环境获取语言环境
/*Locale locale = Locale.getDefault();
System.out.println(locale.getCountry());//输出国家代码
System.out.println(locale.getLanguage());//输出语言代码s //加载国际化资源(classpath下resources目录下的messages.properties,如果是中文环境会优先找messages_zh_CN.properties)
ResourceBundle rb = ResourceBundle.getBundle("resources.messages", locale);
String retValue = rb.getString("101");//101是messages.properties文件中的key
System.out.println(retValue); //信息格式化,如果资源中有{}的参数则需要使用MessageFormat格式化,Object[]为传递的参数,数量根据资源文件中的{}个数决定
String value = MessageFormat.format(retValue, new Object[]{100,200});
System.out.println(value);
*/ }
}

测试代码

package Utils.ResourceUtil;

import org.junit.Test;

public class TestUtils {

    @Test
public void Test1(){
System.out.println(ResourcesUtil.getValue("path", "investigatePicture"));
System.out.println(ResourcesUtil.getValue("path", "auditReprt"));
System.out.println(ResourcesUtil.getValue("path", "jdbc.test"));
     System.out.println(ResourcesUtil.getProperties("path", "hello"));
}
}

结果:

fileLibrary/originalFile/investigatePicture
fileLibrary/originalFile/auditReprt
fileLibrary/originalFile/auditReportWordAndPdf
hellonimei    (证明\是没完,将两行内容合并成一行)

ResourceUtils读取properties文件的更多相关文章

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

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

  2. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  3. 用eclipse做项目中常遇到的问题-如何创建并读取properties文件

    在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...

  4. jsp读取properties文件

    jsp读取properties文件 jsp中读取properties文件,并把值设到js变量中: mpi.properties文件内容: MerchantID=00000820 CustomerEMa ...

  5. java读取.properties文件

    在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...

  6. Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:  InputStream in = this.getClass().getRe ...

  7. Java的Properties类和读取.properties文件

    一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...

  8. Java-马士兵设计模式学习笔记-观察者模式-读取properties文件改成单例模式

    一.概述 1.目标:读取properties文件改成单例模式 二.代码 1.Test.java class WakenUpEvent{ private long time; private Strin ...

  9. Java读取Properties文件的六种方法

    使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...

随机推荐

  1. 【Beanstalkd】Beanstalkd消息队列的安装与使用

    一.Beanstalkd是什么? Beanstalkd是一个高性能,轻量级的分布式内存队列 二.Beanstalkd特性 1.支持优先级(支持任务插队)2.延迟(实现定时任务)3.持久化(定时把内存中 ...

  2. python的一些基本的建议

    一.编码风格 python程序要写的易于阅读 二.python代码的样式规则 遵循PEP8 4个spaces是一次缩排,不允许tabs,不允许混合使用space和tab,方法之间要有一个空行,类之间要 ...

  3. harbor rest api 转graphql api

    原理 实际上就是使用graphql 中的binding,首先基于swagger api 进行schema 生成,后边就是 使用binding 进行graphql 请求api 转换为rest api 请 ...

  4. 打造基于jQuery的智能选择输入框

    UPDATE:修正了在FireFox下显示的问题,重新copy CSS即可 写完这个名字忽然觉得有点标题党的嫌疑,但是又不知道什么样的名字比较合适,那就暂且这样吧. 今天要讲的东西比较简单,其中会用到 ...

  5. SpookyOTP

    https://pypi.python.org/pypi/SpookyOTP/1.1.1 SpookyOTP 1.1.1 Downloads ↓ A lightweight Python 2/3 pa ...

  6. centos6.6 myphpadmin

    基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...

  7. Axis开发Web Service

    可以自动生成代码的 一.Axis环境的安装 1.安装环境 J2SE SDK 1.4,Tomcat 5.0,eclipse 3.2. 2.到 http://xml.apache.org 网站下载Axis ...

  8. IDEA试用期结束以后继续试用(全部失效就更新),IDEA 2018 LICENSE SERVER

    IDEA是一款收费的IDE,但是新用户可以免费试用一段时间,试用期结束可以购买,也可以通过填写License server address来继续使用. 打开IDEA以后,通过Help ----- Re ...

  9. poj-3253-Fence Repair(哈夫曼)

    /* Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19914 Accepted: 6314 Desc ...

  10. leetcode763

    public class Solution { public IList<int> PartitionLabels(string S) { var dic = new Dictionary ...