开发者

java output html code to file

I have a chunk of html code tha开发者_开发问答t should be outputted as a .html file, in java. The pre-written html code is the header and table for a page, and i need to generate some data, and output it to the same .html file. Is there an easier way to print the html code than to do prinln() line by line? Thanks


You can look at some Java libraries for parsing HTML code. A quick Google search tuns up a few. Read in the HTML and then use their queries to manipulate the DOM as needed and then spit it back out. e.g. http://jsoup.org/


Try using a templating engine, MVEL2 or FreeMarker, for example. Both can be used by standalone applications outside of a web framework. You lose time upfront but it will save you time in the long run.


JSP (Java Server Pages) allows you to write HTML files which have some Java code easily embedded within them. For example

<html><head><title>Hi!</title></head><body>
<% some java code here that outputs some stuff %>
</body></html>

Though that requires that you have an enterprise Java server installed. But if this is on a web server, that might not be unreasonable to have.

If you want to do it in normal Java, that depends. I don't fully understand which part you meant you will be outputting line by line. Did you mean you are going to do something like

System.out.println("<html>");
System.out.println("<head><title>Hi!</title></head>");
System.out.println("<body>");
// etc

Like that? If that's what you meant, then don't do that. You can just read in the data from the template file and output all the data at once. You could read it into a multiline text string of you could read the data in from the template and output it directly to the new file. Something like

while( (strInput = templateFileReader.readLine()) != null)
    newFileOutput.println(strInput);

Again, I'm not sure exactly what you mean by that part.


HTML is simply a way of marking up text, so to write a HTML file, you are simply writing the HTML as text to a file with the .html extension.

There's plenty of tutorials out there for reading and writing from files, as well as getting a list of files from a directory. (Google 'java read file', 'java write file', 'java list directory' - that is basically everything you need.) The important thing is the use of BufferedReader/BufferedWriter for pulling and pushing the text in to the files and realising that there is no particular code science involved in writing HTML to a file.

I'll reiterate; HTML is nothing more than <b>text with tags</b>.

Here's a really crude example that will output two files to a single file, wrapping them in an <html></html> tag.

BufferedReader getReaderForFile(filename) {
    FileInputStream in = new FileInputStream(filename);
    return new BufferedReader(new InputStreamReader(in));
}

public void main(String[] args) {
    // Open a file
    BufferedReader myheader = getReaderForFile("myheader.txt");
    BufferedReader contents = getReaderForFile("contentfile.txt");

    FileWriter fstream = new FileWriter("mypage.html");
    BufferedWriter out = new BufferedWriter(fstream);

    out.write("<html>");
    out.newLine();

    for (String line = myheader.readLine(); line!=null; line = myheader.readLine()) {
        out.write(line);
        out.newLine(); // readLine() strips 'carriage return' characters
    }

    for (String line = contents.readLine(); line!=null; line = contents.readLine()) {
        out.write(line);
        out.newLine(); // readLine() strips 'carriage return' characters
    }

    out.write("</html>");
}


To build a simple HTML text file, you don't have to read your input file line by line.

  File theFile = new File("file.html");
  byte[] content = new byte[(int) theFile.length()];

You can use "RandomAccessFile.readFully" to read files entirely as a byte array:

  // Read file function:
  RandomAccessFile file = null;
  try {
    file = new RandomAccessFile(theFile, "r");
    file.readFully(content);
  } finally {
    if(file != null) {
      file.close();
    }
  }

Make your modifications on the text content:

  String text = new String(content);
  text = text.replace("<!-- placeholder -->", "generated data");
  content = text.getBytes();

Writing is also easy:

  // Write file content:
  RandomAccessFile file = null;
  try {
    file = new RandomAccessFile(theFile, "rw");
    file.write(content);
  } finally {
    if(file != null) {
      file.close();
    }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜