I want my Java application to write HTML code in a file. Right now, I am hard coding HTML tags using java.io.BufferedWriter class. For Example:
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("
New PageThis is Body
");bw.close();
Is there any easier way to do this, as I have to create tables and it is becoming very inconvenient?
解决方案
If you want to do that yourself, without using any external library, I think a clean way would be to create a "template.html" file with all the static content, like for example:
/p>
"http://www.w3.org/TR/html4/loose.dtd">
$titlePutting a tag like $tag for any dynamic content and then do something like this:
File htmlTemplateFile = new File("path/template.html");
String htmlString = FileUtils.readFileToString(htmlTemplateFile);
String title = "New Page";
String body = "This is Body";
htmlString = htmlString.replace("$title", title);
htmlString = htmlString.replace("$body", body);
File newHtmlFile = new File("path/new.html");
FileUtils.writeStringToFile(newHtmlFile, htmlString);