读取配置文件的几种方式

2018-12-25  本文已影响0人  wanggs

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);
        }
上一篇下一篇

猜你喜欢

热点阅读