java中读取配置文件的两种方式

方式一:

    @Test
    public void testProperties() throws IOException {
        //方式一:此时文件默认在当前的moudle下
        Properties properties = new Properties();

        FileInputStream fileInputStream = new FileInputStream("jdbc.properties");

        properties.load(fileInputStream);

        String username = properties.getProperty("username");
        String password = properties.getProperty("password");

        System.out.println(username + ":::" + password);


    }

方式二:通过类加载器

        //方式二:配置文件默认识别在当前moudle的src下
        InputStream resource = AppTest.class.getClassLoader().getResourceAsStream("jdbc1.properties");

        properties.load(resource);

        String user = properties.getProperty("username");
        String word = properties.getProperty("password");

        System.out.println(user + ":::" + word);