java java输出html代码到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6459746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:58:21  来源:igfitidea点击:

java output html code to file

javahtmlprinting

提问by Lehan

I have a chunk of html code that 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

我有一大块 html 代码,应该在 java 中作为 .html 文件输出。预先编写的 html 代码是页面的标题和表格,我需要生成一些数据,并将其输出到同一个 .html 文件。有没有比逐行执行 prinln() 更简单的打印 html 代码的方法?谢谢

回答by tskuzzy

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/

您可以查看一些用于解析 HTML 代码的 Java 库。一个快速的谷歌搜索调整了一些。读入 HTML,然后根据需要使用他们的查询来操作 DOM,然后将其吐出。例如http://jsoup.org/

回答by kwo

Try using a templating engine, MVEL2or 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.

例如,尝试使用模板引擎MVEL2FreeMarker。两者都可以由 Web 框架之外的独立应用程序使用。你会在前期浪费时间,但从长远来看它会节省你的时间。

回答by Loduwijk

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

JSP(Java 服务器页面)允许您编写 HTML 文件,这些文件中可以轻松嵌入一些 Java 代码。例如

<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.

尽管这要求您安装了企业 Java 服务器。但如果这是在网络服务器上,那可能不是不合理的。

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

如果你想用普通的 Java 来做,那取决于。我不完全明白你的意思是你将逐行输出哪一部分。你的意思是你要做类似的事情

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.

同样,我不确定你所说的那部分是什么意思。

回答by Charles Goodwin

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.

HTML 只是标记文本的一种方式,因此要编写 HTML 文件,您只需将 HTML 作为文本写入具有 .html 扩展名的文件。

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.

有很多教程可以从文件中读取和写入,以及从目录中获取文件列表。(Google 'java read file', 'java write file', 'java list directory' - 这基本上是你需要的一切。)重要的是使用 BufferedReader/BufferedWriter 将文本拉入和推送到文件中并实现将 HTML 写入文件不涉及特定的代码科学。

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

我要重申;HTML 无非是<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.

这是一个非常粗略的示例,它将两个文件输出到一个文件中,并将它们包装在一个<html></html>标签中。

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

回答by Sascha Wedler

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

要构建一个简单的 HTML 文本文件,您不必逐行读取输入文件。

  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:

您可以使用“ RandomAccessFile.readFully”将文件完全作为字节数组读取:

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