Java code creates HTML files and dynamically adds lines
•
Java
I want to create a log in Java HTML file, where I will dump the error to the HTML table row when the error is captured
public static void main(String[] args) throws IOException { String month = getMonth(1); System.out.println(month); //table(month); addrow("abc"); } public static void table(String Update) throws IOException { //define a HTML String Builder StringBuilder htmlStringBuilder=new StringBuilder(); htmlStringBuilder.append("<html><head><title>Packaging Issues</title></head>"); htmlStringBuilder.append("<body><h1> Files Received </h1>"); htmlStringBuilder.append("<table border=\"1\" bordercolor=\"#000000\">"); htmlStringBuilder.append("<tr><td><b>Cycle</b></td><td>"+Update+"</td></tr>"); htmlStringBuilder.append("<tr><td><b>Version</b></td><td></tr>"); newtable(htmlStringBuilder.toString()); } public static void newtable(String a) { StringBuilder htmlStringBuilder=new StringBuilder(); htmlStringBuilder.append(a); htmlStringBuilder.append("<h1> Issues Found </h1>"); htmlStringBuilder.append("<table border=\"1\" bordercolor=\"#000000\">"); htmlStringBuilder.append("<tr><td><b>Type</b></td>"); htmlStringBuilder.append("<td><b>Field</b></td>"); htmlStringBuilder.append("<td><b>Issue</b></td></tr>"); addrow(htmlStringBuilder.toString()); // WriteToFile(htmlStringBuilder.toString(),"log.html"); } public static void addrow(String a) { try { StringBuilder htmlStringBuilder=new StringBuilder(); htmlStringBuilder.append("<tr><td><b>"+a+"</b></td>"); htmlStringBuilder.append("<td><b>Field</b></td>"); htmlStringBuilder.append("<td><b>Issue</b></td></tr>"); htmlStringBuilder.append("</table></body></html>"); WriteToFile(htmlStringBuilder.toString(),"log.html"); } catch (IOException e) { e.printStackTrace(); } }
This is the writetofile method:
public static void WriteToFile(String fileContent,String fileName) throws IOException { String projectPath = "/home/abc"; String tempFile = projectPath + File.separator+fileName; File file = new File(tempFile); // if file does exists,then delete and create a new file //write to file with OutputStreamWriter OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile()); Writer writer=new OutputStreamWriter(outputStream); writer.write(fileContent); writer.close(); }
If there are other ways, please share Thank you for your support
Solution
The constructor fileoutputstream (file) overwrites the contents of the file Use this constructor instead
FileOutputStream fos =new FileOutputStream(file,true) ;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码