ResourceUtils读取properties文件

注意:

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

path.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

 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"));
    }
}

结果:

fileLibrary/originalFile/investigatePicture
fileLibrary/originalFile/auditReprt
fileLibrary/originalFile/auditReportWordAndPdf

时间: 2024-08-05 03:04:12

ResourceUtils读取properties文件的相关文章

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

一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享. 二.项目环境介绍 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Id

在JSP页面中读取properties文件

在做web开发时,经常遇到要修改一下配置信息.如果把这些配置信息写在代码中,后期的维护便会比较麻烦.所以,一般都是把配置信息写在配置文件里面. 在JSP文件中,如果想要调用properties文件中的变量,则要在有文件中引入 java.util.ResourceBundle 类: <%@ page contentType="text/html; charset=UTF-8" import="java.util.ResourceBundle" %> 已知配

Java读取Properties文件的六种方法

使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2.使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb

转载:java基础学习总结——java读取properties文件总结

java基础学习总结--java读取properties文件总结 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的

Java读取.properties文件

例1: 创建一个config文件夹 config文件夹中有一个Properties.properties文件 内容为: capitalLetter=ABCDE smallLetter=abcde 注意:config文件夹与包含Test类的包为同一级 import java.io.IOException; import java.util.Properties; public class Test { public static void main(String[] args) { Propert

java读取.properties文件及解决中文乱码问题

Java项目中有些信息(例如web的配置信息)可能要放在.properties文件中,那我们应该怎么来读取它们呢,下面给出一个工具类做一说明,并解决了中文乱码问题: 1.其中config.properties文件信息如下: name=\u843D\u82B1\u6709\u610Fwang王 str=\u6D41\u6C34\u65E0\u60C5 boolean=true 2.PropertiesUtil工具类读取.properties文件 import java.io.BufferedInp

用java读取properties文件--转

今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名如(file.properties) InputStream in = null; Properties pros = new Properties(); tr

Java实现动态加载读取properties文件

问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties prop = new Properties(); prop.load(classLoader.getResourceAsStream("/Application.properties")); 会发现修改了.properties后,即使重新执行,读入的仍为修改前的参数.此问题的原因在于Cla

读取Properties文件以及中文乱码问题

在java类中常见的读取Properties文件方式,是使用Properties.load(inputStream);的方式但是常常出现中文乱码问题,这就很尴尬了 public synchronized void load(InputStream inStream) throws IOException { load0(new LineReader(inStream)); } 看了很久才发现,还有一个重载的方法, 它的参数是Reader,如下: public synchronized void