读取resources目录下文本文件内容:
例如文件存在:/resources/geo/map.json
FileUtil
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FileUtils {
public static String getContent(String filePath){
String res = "";
if(StringUtils.isEmpty(filePath)){
return res;
}
try {
Resource resource = new ClassPathResource(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),"UTF-8"));
StringBuffer sb = new StringBuffer();
String str = "";
while((str=br.readLine())!=null) {
sb.append(str);
}
res = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
代码片段
String path = "geo/map.json";
String geoJson = FileUtils.getContent(path);