Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create html file and add rows dynamically

Tags:

java

I wanted to create log.html file using java wherein i'll dump errors into html table-row as and when errors are caught. I created table columns but when i am passing anything to it then my data is overwriting column names instead of appending row. My java code is as follows:

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();
    }
}

Here's 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();

}
like image 911
shreya Avatar asked Oct 31 '22 10:10

shreya


1 Answers

The constructor FileOutputStream(file) overwrites the contents of the file. Use this constructor instead

FileOutputStream fos =new FileOutputStream(file, true) ;
like image 133
Rohan Kamat Avatar answered Nov 15 '22 05:11

Rohan Kamat