美文网首页
读取配置文件的几种方式

读取配置文件的几种方式

作者: wanggs | 来源:发表于2018-12-25 23:33 被阅读0次

    1.

    image.png
            try {
                //定义一个properties对象
                Properties prope = new Properties();
                InputStream inputStream = ReadProper.class.getClassLoader().getResourceAsStream("org/coody/verification/param.properties");
    
                prope.load(inputStream);
                System.out.println(prope.get("name"));
            } catch (Exception e) {
    
                throw new RuntimeException("读取配置文件失败:" + e);
            }
    

    2.

    image.png
     /**
         * 1.只能读取properties文件
         * 2.只能读取不能写入
         * 3.只能读取类路径下的
         * 注:不写扩张名
         */
        private static ResourceBundle lStrings = ResourceBundle.getBundle("org.coody.verification.param");
        
        public static void main(String[] args) throws IOException {
            String msg = lStrings.getString("name");
        }
    

    3.

           try {
                //定义一个properties对象
                Properties prope = new Properties();
                //绝对不能用因为tomcat编译war包的时候路径不对
                InputStream inputStream = new FileInputStream("src/org/coody/verification/param.properties");
                prope.load(inputStream);
                System.out.println(prope.get("name"));
            } catch (Exception e) {
    
                throw new RuntimeException("读取配置文件失败:" + e);
            }
    

    相关文章

      网友评论

          本文标题:读取配置文件的几种方式

          本文链接:https://www.haomeiwen.com/subject/xtqclqtx.html