getResourceAsStream() is returning null. Properties file is not loading

前端 未结 6 1278
眼角桃花
眼角桃花 2020-12-09 16:23

I am trying to load properties file. Here is my structure

\"Directory

Now i am trying to lo

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 16:45

    Please put your property file in /src/main/resources folder and load from ClassLoader. It will be fix.

    like

     /src/main/resources/test.properties
    
    
    
    Properties properties = null;
    
    try {
        properties = new Properties();
        InputStream resourceAsStream =  Test.class.getClassLoader().getResourceAsStream("test.properties");
        if (resourceAsStream != null) {
            properties.load(resourceAsStream);
        }
    
    
    } catch (IOException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题