读取配置文件properties的方法

来源:互联网 发布:爬山虎公司知乎 编辑:程序博客网 时间:2024/05/19 23:26

方法一:通过Class的getResourceAsStream方法和Properties的load方法读取*.properties文件的内容;

    @Test     //读取配置文件    public void readProperties() throws IOException {        //通过Class的getResourceAsStream方法获取根目录下hibernate.properties文件的输入流        InputStream inputStream = Reflect1.class.getResourceAsStream("/hibernate.properties");        Properties properties = new Properties();         //将properties文件中的属性读入到对象properties中        properties.load(inputStream);        //get方法获取对应的值        String username = (String) properties.get("hibernate.connection.username");        System.out.println(username);        inputStream.close();    }
原创粉丝点击