Printing an HTML Table : Print To Files « File Input Output « Java






Printing an HTML Table

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;

public class HTMLDemo {

  public static void main(String[] a) throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter("test.html"));
    DecimalFormat ff = new DecimalFormat("#0"), cf = new DecimalFormat(
        "0.0");
    pw.println("<TABLE BORDER><TR><TH>Fahrenheit<TH>Celsius</TR>");
    for (double f = 100; f <= 400; f += 10) {
      double c = 5 * (f - 32) / 9;
      pw.println("<TR ALIGN=RIGHT><TD>" + ff.format(f) + "<TD>"
          + cf.format(c));
    }
    pw.println("</TABLE>");
    pw.close(); // Without this, the output file may be empty
  }
}
           
       








Related examples in the same category

1.Printing Numbers to a Text File
2.illustrates using a PrintWriter to handle console output