文件需要放在resource下,打成jar包后仍然可以读取到
1. 各种环境都能读取。(通用)
ClassPathResource classPathResource = new ClassPathResource("word.txt");
InputStream inputStream =classPathResource.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader br=new BufferedReader(in);
2. (通用)
BufferedReader br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("word.txt"), "UTF-8"))
3.
InputStream inputStream = this.getClass().getResourceAsStream("/excleTemplate/test.xlsx");
或者
4.
File file = ResourceUtils.getFile("classpath:word.txt");
InputStream inputStream = new FileInputStream(file);
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader br=new BufferedReader(in);
5.
BufferedReader br=new BufferedReader(new FileReader("src/main/resources/word.txt"));
或者
BufferedReader br=new BufferedReader(new FileReader(ResourceUtils.getFile("classpath:word.txt")));
6. (通用)
@Autowired
ResourceLoader resourceLoader;
Resource resource = resourceLoader.getResource("classpath:resource.properties");
InputStream is = resource.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);