zoukankan      html  css  js  c++  java
  • 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中各种不同方式来加载文件时候都有默认的主路径(也可以说成根目录)

    根目录也就决定了这个路径到底应该怎么写,才能被程序识别。我现在还不得而知,先记录下这些区别。

    如果大家有什么想指正、教育我的地方,欢迎指出,小弟想知道到底是怎么样子的。

  • 相关阅读:
    已经完全付款的发票仍然可以选择并进行零金额的付款
    How to fix Safari can't download .DMG
    WPF学习笔记系列
    无废话WPF系列17:数据模版
    Mac 用GUI工具打开隐藏文件
    无废话WPF系列19:MVVM简单介绍
    ASP.NET MVC3实战系列(二):面向接口编程,提高系统可测试性。
    Windows文件被占用解决办法
    无废话WPF系列18:控件模版
    ASP.NET MVC3实战系列(三):MVC3中使用依赖注入(IOC)
  • 原文地址:https://www.cnblogs.com/yanwenxiong/p/5595255.html
Copyright © 2011-2022 走看看