0
밥상방법 그러나,
내가 String 변수에 공급 될 템플릿 파일을 사용하고 문자열 readAllBytes에서 줄 바꿈을 유지합니다.
원본 파일의 줄 바꿈을 어떻게 보존하여 출력 파일에도 이러한 줄 바꿈이 포함되어 있습니까?
코드
InputStream in = classLoader.getResourceAsStream("templates/reportTemplate.html");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//File htmlTemplateFile = new File();
// String htmlString = FileUtils.read(classLoader.getResourceAsStream("templates/reportTemplate.html");)
String body = data;
StringBuilder result = new StringBuilder("");
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
File newHtmlFile = new File(fileLocation + "/report.html");
FileUtils.writeStringToFile(newHtmlFile, result.toString().replace("$body", body));
Path path = Paths.get(fileLocation + "/report.html");
Charset charset = StandardCharsets.ISO_8859_1;
String content = new String(Files.readAllBytes(path),charset);
// Not working :: String newLineChar = System.getProperty("line.separator");
// Not working :: content = content.replaceAll("\\n", newLineChar);
content = content.replaceAll("\\$highrisk", Integer.toString(highrisk));
content = content.replaceAll("\\$criticalrisk", Integer.toString(criticalrisk));
content = content.replaceAll("\\$mediumrisk", Integer.toString(mediumrisk));
content = content.replaceAll("\\$lowrisk", Integer.toString(lowrisk));
Files.write(path, content.getBytes(charset));
템플릿 파일 문제가있다
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Risk', 'Risk Meter'],
['Medium', $mediumrisk],
['Critical', $criticalrisk],
['High', $highrisk],
['Low', $lowrisk],
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Risk Coverage', 'width':550, 'height':400};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>myAp Report</title>
</head>
<body>
<div class="container" style="float: left;">
<h2>Report from myApp</h2>
<p>This report contains summary of scan results from my app</p>
$body
</div>
<div id="piechart"></div>
</body>
</html>
참고로, replaceAll ("\\ $ highrisk"대신에 replace ("$ highrisk"')를 사용할 수 있습니다. – shmosel
원래 개행 문자가 있었거나 삭제되었다는 증거는 없습니다. [mcve]를 입력하십시오. – shmosel
@shmosel 질문은 토큰을 대체하는 것에 관한 것이 아닙니다. fine.Also가있는 파일은 linebreaks가 있지만 String으로 공급되고 파일에 다시 기록되면 Breaks가 보존되지 않는 파일 일 수 있습니다. – xtechkid