Load the text under the resource file in SpringBoot
In the afternoon, there was a problem in Sun Dajun's project; the program written by Dajun was written and run locally, but after packaging, it would cause problems when deployed to the server. . .
After my research and various searches on the website, I finally solved it. . .
Load files under resource
使用
方法1: File resourceFile = ResourceUtils.getFile("classpath:xxx.txt");
方法2: Resource resource = new ClassPathResource("xxx.txt");
File file = resource.getFile();
但是在jar中使用文件可能导致获取不到,出现异常;
所以在jar(linux)中使用的时候最好是使用文件流来处理文件;
方法3: Resource resource = new ClassPathResource("xxx.txt");
File file = resource.getInputStream();