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

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

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

  • 相关阅读:
    大型网站技术架构(八)--网站的安全架构(转)
    大型网站技术架构(七)--网站的可扩展性架构(转)
    大型网站技术架构(六)--网站的伸缩性架构(转)
    大型网站技术架构(五)--网站高可用架构(转)
    大型网站技术架构(四)--网站的高性能架构(转)
    HTML中的空元素
    jetbrains 全家桶,Pycharm激活码,PHPStorm激活码,WebStorm激活码,Goland激活码,Clion激活码,intellij idea激活码(亲测可用)
    legend3---22、通过html,可以使php、vue、jquery、js达成完美的配合
    IlluminateDatabaseQueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
    template cannot be keyed. Place the key on real elements instead.
  • 原文地址:https://www.cnblogs.com/yanwenxiong/p/5595255.html
Copyright © 2020-2023  润新知