zoukankan      html  css  js  c++  java
  • 如何读取path配置文件里面的属性值

    path.properties文件里面的数据一般都是以’key=value’的形式书写的
    className=com.lovo.bean.Teacher
    methodName=info
    以上,classname相当于key,com.lovo.bean.Teacher相当于key对应的value
    如果要读取path文件里面的值
    必须要使用到java.util下的Properties这个类
    因为需要读取文件,所以还需要用到IO流 InputStream 中的FileInputStream

    public class TestProperties {
        // 创建一个静态常量保存配置文件里面的key属性
        private static final String CLASSNAME_KEY = "className";
        private static final String METHODNAME_KEY = "methodName";
        //用于获取配置文件中所有的属性值
        private static String[] sArray = new String[]{CLASSNAME_KEY, METHODNAME_KEY};
    
        public static void main(String[] args) {
            String className = getClassName();
            //System.out.println(className);
    
            List<String> sList = getPropertiesValue();
            //System.out.println(sList.toString());
        }
    
        /**
         * 获取path文件中指定key属性的value
         *
         * @return value
         * <p>
         * 例如以下方法:获取path配置文件中‘className’对应的值
         */
        public static String getClassName() {
            //1. 实例化一个Properties对象pps
            Properties pps = new Properties();
            //2. 实例化一个输入流的对象is
            InputStream is = null;
            //3. 创建一个字符串对象用于保存pps获取的返回值
            String className = null;
    
            try {
                // 通过path.properties文件路径读取配置文件
                is = new FileInputStream("res/path.properties");
                // 通过pps加载is读取的数据
                pps.load(is);
                // 调用getproperties()方法获取key对应的值,即CLASSNAME_KEY对应的值
                className = pps.getProperty(CLASSNAME_KEY);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            // 最后返回CLASSNAME_KEY对应的value
            return className;
        }
    
        /**
         * 获取path文件中所有key属性的value
         *
         * @return value
         * <p>
         * 例如以下方法:获取path配置文件中‘className’,‘methodName’对应的值
         */
        public static List<String> getPropertiesValue() {
            List<String> list = new ArrayList<>();
    
            Properties pps = new Properties();
            InputStream is = null;
    
            try {
                is = new FileInputStream("res/path.properties");
    
                pps.load(is);
                String s = null;
                for (String ss : sArray) {
                    s = pps.getProperty(ss);
                    list.add(s);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return list;
        }
    }
  • 相关阅读:
    注意 定义类的时候 不要null 别忘了new 实例化 否则报错
    如果通过key获取dictionary里面的value
    C#邮箱身份验证
    时间选择器 可以选择日期和时间
    C#的Timer解析
    使用微软的组件发邮件
    将html代码写入临时文件夹下面的 然后发来给webbrowser使用
    c#中DataGridView 如何设置 才能选中一行 设置鼠标事件
    lumisoft 获取邮件的方法
    C# 动态添加CheckedListBox的选项,并设置选项为 选中? 怎么做?
  • 原文地址:https://www.cnblogs.com/fjie/p/13625360.html
Copyright © 2011-2022 走看看