yan061

导航

JAVA8 获取properties文件的内容

resource目录下有一个test.properties文件,里面有以下内容,

 

方式一:

 

我们可以用过ResourceBundle来获取到对应的内容

 

    @Test
    public void readData() {
        ResourceBundle bundle = ResourceBundle.getBundle("test");
        String username = bundle.getString("username");
        System.out.println("username = " + username);

    }

输出为

 

方式二:

通过Properties类来获取

  @Test
    public void test1() throws IOException {

        Properties properties = new Properties();
        properties.load(new FileInputStream(new File("test.properties")));
        String name = properties.getProperty("name");
        System.out.println(name);

    }

 

方式三:

也是通过properties类来获取

  @Test
    public void test2() throws IOException {
        Properties properties = new Properties();
        ClassLoader classLoader = PropertiesTest.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("test.properties");
        properties.load(inputStream);
        String name = properties.getProperty("name");
        System.out.println(name);
    }

 

posted on 2022-03-10 18:51  yan061  阅读(175)  评论(0编辑  收藏  举报