读取文件
private String getFileContent(String filePath) {
File template = new File(filePath);
if (!template.exists()) {
LOGGER.error("模板文件不存在:{}", filePath);
return null;
}
BufferedReader bufferedReader = null;
StringBuilder sb = new StringBuilder();
try {
bufferedReader = new BufferedReader(new FileReader(template));
String temp;
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp);
}
} catch (FileNotFoundException e) {
LOGGER.error("文件未找到异常:{}", filePath);
return null;
} catch (IOException e) {
LOGGER.error("读取文件异常:{}", filePath);
return null;
} finally {
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
LOGGER.error("关闭文件流异常:{}", filePath);
}
}
}
return sb.toString();
文件写入
private File createNewReport(boolean isWeeklyReport, String templateContent, String content, int corpId) {
String reportDir = templateDir + corpId + File.separator + new SimpleDateFormat("yyyyMMdd").format(new Date()) + File.separator;
File newHtml;
BufferedWriter bw = null;
try {
File dir = new File(reportDir);
if (!dir.exists()) {
dir.mkdirs();
}
if (!newHtml.exists()) {
newHtml.createNewFile();
}
bw = new BufferedWriter(new FileWriter(newHtml));
bw.write(reportContent);
bw.flush();
} catch (IOException e) {
LOGGER.error("创建报告失败", e);
} finally {
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
LOGGER.error("关闭BufferedWriter失败", e);
}
}
}
return newHtml;
}