zoukankan      html  css  js  c++  java
  • 读取src下的属性文件的某个key值

    读取src下的属性文件的某个key值

     /**
         * 读取src下某个属性文件中的某个key所对应的value
         *
         * @param args
         *
         */

        public static void main(String[] args) {

            String key = "file.upload.folder";
            String propertiesFileFullName = "myProperty.properties";
            String pbundle = "myProperty";

            fun1(propertiesFileFullName, key);
            fun2(propertiesFileFullName, key);
            fun3(pbundle, key);
        }

        // 方法一
        public static void fun1(String propertiesFileFullName, String key) {
            Properties pop = new Properties();
            InputStream fs = null;
            try {
                fs = ReadOneValue.class.getClassLoader().getResourceAsStream(
                        propertiesFileFullName);
                pop.load(fs);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String value = pop.getProperty(key);
            System.out.println("属性文件的上传路径为:" + value);
        }

        // 方法二
        public static void fun2(String propertiesFileFullName, String key) {
            Properties pop = new Properties();
            String classpath = Thread.currentThread().getContextClassLoader()
                    .getResource("").getPath();

            FileInputStream fs = null;
            try {
                fs = new FileInputStream(classpath + "/" + propertiesFileFullName);
                pop.load(fs);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String value = pop.getProperty(key);
            System.out.println("读取到的值为:" + value);
        }

        // 方法三
        public static void fun3(String filename, String key) {
            Locale locale = Locale.getDefault();
            ResourceBundle localResource = ResourceBundle.getBundle(filename,
                    locale);
            String value = localResource.getString(key);
            System.out.println("读取到的值为:" + value);
        }
    }

  • 相关阅读:
    Spring MVC返回多重的Json数据
    Eclipse Maven项目中修改JDK版本
    Maven的使用笔记
    Windows下Redis主从配置出现Writing to master:Unknow error
    Java开发必会的Linux命令(转)
    使用maven引入slf4j、logback时发生冲突
    使用SSM框架搭建JavaWeb,使用Junit测试时遇到CannotGetJdbcConnetionException
    HTTP基础
    express 热启动 静态文件部署 跨域解决 调试
    github+git提交 基础用法
  • 原文地址:https://www.cnblogs.com/yangy608/p/2726508.html
Copyright © 2011-2022 走看看