1.静态文件存在于文件系统中(有具体的路径) SpringBoot的配置文件一般写在application.yml中,如何自定义将一些额外的属性用json存储起来,以方便读取和修改呢?主要使用两个类库,Fastjson和Common-io。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public void readConfigFile () { try { File file=new File (configfile); String content= FileUtils.readFileToString(file,"UTF-8" ); JSONObject jsonObject=JSONObject.parseObject(content); this .config=jsonObject; }catch (IOException e){ logger.error("ConfigFile.readConfigFile" ,e); } } private boolean writeConfigFile () { try { if (config!=null ){ String json=JSONObject.toJSONString(config); File file=new File (configfile); FileUtils.writeStringToFile(file,json,"UTF-8" ,false ); return true ; } }catch (IOException e){ logger.error("ConfigFile.wirteConfigFile" ,e); } return false ; }
参考文章: 1.java——读取JSON文件 2.使用FileUtils简化你的文件操作 3.读取Json配置文件 4.fastjson的基本使用方法 5.Java IO流学习总结七:Commons IO 2.5-FileUtils 6.FileUtils简化你的文件操作
另一种方法: 使用Runable接口,其实和上面的都是差不多的。
参考文章: 1.SpringBoot JSON文件读取 2.【Java并发编程】之六:Runnable和Thread实现多线程的区别(含代码) 3.Springboot 读取项目下的Json文件成对象 4.Spring Boot 从Json静态文件中读取数据
2.资源目录下的静态文件(存放于资源resources目录下) 如果静态文件存在于resources目录下,可以通过ClassPathResource加载相应目录下的文件并读取其中的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.proheng.gis.ApiUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import java.io.BufferedReader;import java.io.InputStreamReader;public class ApiUtils { private static final Logger loger= LoggerFactory.getLogger(ApiUtils.class); public static String readFile (String fileName) { StringBuilder stringBuilder=new StringBuilder (); try { Resource resource = new ClassPathResource (fileName); BufferedReader br = new BufferedReader (new InputStreamReader (resource.getInputStream())); String temp = null ; while ((temp = br.readLine()) != null ) { stringBuilder.append(temp); } }catch (Exception e){ loger.error("ApiUtils.readFile" ,e); return "" ; } return stringBuilder.toString(); } }
参考文章: 1.springboot获取静态资源的路径 2.Java读取文件内容与字符串保存成文件的操作 3.Spring Boot 读取静态文件 4.Spring Boot 读取静态资源文件
注意事项 使用ClassPathResource进行resource文件读取,在开发的时候没有问题,但是打包部署到服务器上之后,就会出现问题了。
1 cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/cloud/dev/PhEMSJava/PHEMSsimulation.jar!/BOOT-INF/classes!/inp/pipeline.inp
1 2 3 ClassPathResource classPathResource = new ClassPathResource (inpFilePath+"pipeline.inp" );File inpFile=classPathResource.getFile();
【解决方法】 将文件读取方式,改为流读取方式
1 2 3 4 ClassPathResource classPathResource = new ClassPathResource (inpFilePath+"pipeline.inp" );inpStream=classPathResource.getInputStream();
参考文章: 1.SpringBoot打包后无法读取到resource下的资源文件 2.Spring boot 打包jar后无法读取resource下的配置文件 通过文件流的读取方式,代码中将*.conf文件拷贝至jar外的临时文件夹下,然后再读取临时文件夹下的*.conf文件。 3.问题解决:cannot be resolved to absolute file path because it does not reside in the file system: jar 1.使用resource.getInputStream()读取文件内容。2.缓存到临时文件,使用临时文件路径进行读取。 4.SpringBoot项目jar启动异常:java.io.FileNotFoundException: file:/xxx/xxx.jar!/BOOT-INF/classes!/xxx.yml