When to use “getResourceAsStream” method?

后端 未结 4 1278
死守一世寂寞
死守一世寂寞 2020-12-11 06:39

I was confused using the said method because while loading some properties file people are following a different approaches...

Properties prop 
 = new Proper         


        
4条回答
  •  春和景丽
    2020-12-11 07:01

    When you are reading a file from the Jar. Please use classloader's getResource or getResoureAsstream. Find the below code snippet to read a file from Jar. The above approaches cannot read a file from jar.

        InputStream in = this.getClass().getClassLoader()
                .getResourceAsStream("com/net/resources/config.properties");
    
        InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("com/net/resources/config.properties");
    
        URL url = this.getClass().getClassLoader()
                .getResource("com/net/resources/config.properties");
    

提交回复
热议问题