1、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用)
File file = new File("src/main/resources/resource.properties");
@Testpublic void testReadFile2() throwsIOException {
File file= new File("src/main/resources/resource.properties");
FileInputStream fis= newFileInputStream(file);
InputStreamReader isr= newInputStreamReader(fis);
BufferedReader br= newBufferedReader(isr);
String data= null;while((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
isr.close();
fis.close();
}
2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)
File file = ResourceUtils.getFile("classpath:resource.properties"