maven工程中,要加載classpath下的文件並以InputStream的形式返回,一般使用的方法是maven
InputStream inputStream = Test.class.getClassLoader.getResourceAsStream("config.properties");
今天無心中發現另外一種方法,以下:spa
@Value("classpath:/config.properties")
Resource configFile;
獲得Resource對象後,使用getInputStream()方法便可得到,固然也能夠獲得指向文件的File對象,代碼以下:code
InputStream inputStream = configFile.getInputStream();
File file = configFile.getFile();