废话少说 放码
/**
* 读取Resources下资源文件
* jar包时 linux下也可用
* @param filePath 文件路径 例如 /topology/color.json 注意/
* @return String 文件内容
*/
private static String readFileStreamToStr(String filePath) {
InputStream is = null;
BufferedReader br = null;
try {
is = FileReadUtils.class.getResourceAsStream(filePath);
if (null == is) {
log.error("readFile: file is not found, file path: {}", filePath);
return null;
}
br = new BufferedReader(new InputStreamReader(is));
String s;
StringBuilder sb = new StringBuilder();
while ((s = br.readLine()) != null) {
sb.append(s).append("\r\n");
}
return sb.toString();
} catch (IOException e) {
log.error(e.getCause().toString());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
log.error("error : FileReadUtils-readLinuxFileToStr-close InputStream : {}", e.getMessage());
}
}
if (br != null) {
try {
is.close();
} catch (IOException e) {
log.error("error : FileReadUtils-readLinuxFileToStr-close BufferedReader : {}", e.getMessage());
}
}
}
return null;
}