java springboot框架,编译为jar包后也能读取resources/templates目录下的文件
InputStream simulatorServiceInputStream = this.getClass().getResourceAsStream("/templates/simulatorServiceTemplate.txt");
StringBuilder simulatorServiceContentBuilder = new StringBuilder();
byte[] fileContentBytes = new byte[1024];
while(true){
assert simulatorServiceInputStream != null;
if ((simulatorServiceInputStream.read(fileContentBytes)) == -1) break;
int fileContentLength = 0;
//这里是为了删除结束符,否则会在文件中写入一堆null
for (byte b : fileContentBytes) {
if (b != 0) {
fileContentLength++;
} else {
// 遇到结束符0,跳出循环
break;
}
}
String strRead = new String(fileContentBytes,0,fileContentLength,StandardCharsets.UTF_8);
simulatorServiceContentBuilder.append(strRead);
fileContentBytes = new byte[1024];
}
String simulatorServiceContent = simulatorServiceContentBuilder.toString();
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。