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代码应该作为.html文件输出,在java中。预先编写的html代码是页面的标题和表格,我需要生成一些数据,然后将其输出到同一个.html文件中。是否有一种更简单的方法来打印html代码而不是逐行执行prinln()?谢谢
5 个解决方案
#1
0
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/
您可以查看一些Java库来解析HTML代码。一个快速的谷歌搜索收集了一些。读入HTML,然后根据需要使用他们的查询操作DOM,然后将其吐出。例如http://jsoup.org/
#2
0
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.
例如,尝试使用模板引擎,MVEL2或FreeMarker。两者都可以由Web框架之外的独立应用程序使用。你提前浪费时间,但从长远来看,这将节省你的时间。
#3
0
JSP (Java Server Pages) allows you to write HTML files which have some Java code easily embedded within them. For example
JSP(Java Server Pages)允许您编写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服务器。但如果这是在Web服务器上,那可能不合理。
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.
同样,我不确定你的意思究竟是什么意思。
#4
0
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只不过是带标签的文字 。
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>");
}
#5
0
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();
}
}
#1
0
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/
您可以查看一些Java库来解析HTML代码。一个快速的谷歌搜索收集了一些。读入HTML,然后根据需要使用他们的查询操作DOM,然后将其吐出。例如http://jsoup.org/
#2
0
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.
例如,尝试使用模板引擎,MVEL2或FreeMarker。两者都可以由Web框架之外的独立应用程序使用。你提前浪费时间,但从长远来看,这将节省你的时间。
#3
0
JSP (Java Server Pages) allows you to write HTML files which have some Java code easily embedded within them. For example
JSP(Java Server Pages)允许您编写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服务器。但如果这是在Web服务器上,那可能不合理。
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.
同样,我不确定你的意思究竟是什么意思。
#4
0
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只不过是带标签的文字 。
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>");
}
#5
0
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();
}
}