简体   繁体   中英

Output as HTML instead of plain text

Instead of the old System.out.println(); for debugging; would it be possible to output as html and have the (text)results of my execution a little more expressive.

That way I would be able to do things like

System.out.println("<b>...</b> ..."); 

or perhaps more interestingly write out tables 'n stuff. Colors would also be an interesting dimension to have in debugging/producing output.

How could one do this (effectively) in eclipse?

Thanks :)

What you should do is use your own Logger class, such as this:

public class HtmlLogger {

  private static File file = new File("outputfile.html");
  private static DataOutputStream out;

     private static void start() {
         try {
          out = new DataOutputStream(new FileOutputStream(file, true));
       } catch (IOException ex) {...}
     }

     // Returns size in Kb
     private static long fileSize() {
         return file.length() / 1024;
     }

     // You can call this method whatever you like.
     public static void html(String line) {
         if (out == null) start();
         synchronized (out) {
             try {
               out.writeBytes(line + System.getProperty("line.separator"));
               out.flush();
           } catch (IOException ex) {...}
        }
     }
}

So what you can do is import a static HtmlLogger, import static HtmlLogger.*; and output HTML per line like this: html("<p>This is a paragraph</p>");

I'm sure there are some HTML libraries out there, but this is a similar approach to PHP when you're manually echo'ing HTML code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM