I want to create a html page dynamically everytime I click on The button.
What I have tried:
public class CreateHtmlDocument {
public static void main(String[] args)
{
int i=0;
try {
//define a HTML String Builder
StringBuilder htmlStringBuilder=new StringBuilder();
//append html header and title
htmlStringBuilder.append("<html><head><title>hello </title></head>");
//append body
htmlStringBuilder.append("<body>");
//append table
htmlStringBuilder.append("");
//append row
htmlStringBuilder.append("");
//append row
htmlStringBuilder.append("");
//append row
htmlStringBuilder.append("");
//close html file
htmlStringBuilder.append("
TestId | TestName | TestResult |
001 | Login | Passedsssss |
002 | Logout | Passedssss |
</body></html>");
//write html string content to a file
i++;
String a="t.html";
WriteToFile(htmlStringBuilder.toString(),a);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void WriteToFile(String fileContent, String fileName) throws IOException {
String projectPath = System.getProperty("user.dir");
String tempFile = projectPath + File.separator+fileName;
System.out.println(tempFile);
File file = new File(tempFile);
// if file does exists, then delete and create a new file
if (file.exists()) {
try {
File newFileName = new File(projectPath + File.separator+ "backup_"+fileName);
file.renameTo(newFileName);
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//write to file with OutputStreamWriter
OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile());
Writer writer=new OutputStreamWriter(outputStream);
writer.write(fileContent);
writer.close();
}
}