java获取src下文件

时间:2021-10-31 18:43:55

方式一:

  

InputStream in = Test.class   
.getResourceAsStream(
"/env.properties");
URL url
= Test.class.getResource("env.properties") ;

 说明:env.properties文件在src的根目录下,文件名前有斜杠

方式二:

InputStream in = Test.class.getClassLoader()    
.getResourceAsStream(
"env.properties");
URL url
= Test.class.getClassLoader().getResource("env.properties") ;

方式三:

InputStream in = Thread.currentThread().getContextClassLoader()  
.getResourceAsStream(
"ExcelModeMappingl.xml");

原贴地址:https://yq.aliyun.com/articles/40852