Kerry G
Kerry G

Reputation: 947

writing text to an html file type

I am trying to write the result of the following method to a webpage. I think it is possible to do, I am just having trouble figuring out exactly how to do it. Any help would be very appreciated. Thanks.

...
      System.out.println("Final Register");
      for (int i=0; i < ch.length; i++)
        {
         System.out.println(cd.getDenomLiteralAt(i)+" "+cd.getDenomNumAt(i));
        }
   }

...

Upvotes: 3

Views: 22203

Answers (2)

MaVRoSCy
MaVRoSCy

Reputation: 17839

In java there are many ways to write data on a file. To write text data the easiest way is by using a BufferedWriter.

See demo below

FileWriter fWriter = null;
BufferedWriter writer = null;
try {
    fWriter = new FileWriter("fileName.html");
    writer = new BufferedWriter(fWriter);
    writer.write("<span>This iss your html content here</span>");
    writer.newLine(); //this is not actually needed for html files - can make your code more readable though 
    writer.close(); //make sure you close the writer object 
} catch (Exception e) {
  //catch any exceptions here
}

Upvotes: 6

Dan
Dan

Reputation: 3367

All you need to do is know the path to the public HTML file of your webserver.

Run the Java code and write to a file below that path. It's no different than writing to any other file on a machine, except this one the world can see. Only thing is, if your Java will be creating the file, you should be sure to set protective enough permissions for the file. 0755 is moderately safe.

Upvotes: 1

Related Questions