我在运行程序时遇到了麻烦,该程序每次运行时都会创建html模板
问题描述:
我正在用java编写一个程序,该程序每次运行时都会创建一个HTML模板。它读取由四个标题和段落组成的输入生物文本文件,每个文件一行。它采用txt并将标题和段落存储到并行数组中。然后我将数组写入一个HTML文件。问题是,当我运行它时,没有输入到文件中,它也表示读取器从未关闭。这是我的程序和输出。谢谢你的任何建议。我在运行程序时遇到了麻烦,该程序每次运行时都会创建html模板
package edu.txstate.cs3320.qwt1;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
public class bio2 {
private static final String OUTPUT_FILE = "./iofiles/bio.html";
private static final String INPUT_FILE = "./iofiles/bio.txt";
private static final String TITLE = "My LIfe";
private static BufferedWriter writer = null;
private static ArrayList <String> headings = new ArrayList <>();
private static ArrayList <String> paragraphs = new ArrayList <>();
//methods before main
private static void initialize (String fileName) {
try {
writer = new BufferedWriter(new FileWriter(fileName));
}catch(IOException e) {
e.printStackTrace();
}
}
private static void readParagraphs(String inputFileName) {
final int numberOfParagraphs = 4;
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFileName));
for(int i = 0; i<numberOfParagraphs;i++) {
String headerLine = reader.readLine();
if(headerLine != null) headings.add(headerLine);
String paragraphLine = reader.readLine();
if(paragraphLine != null) paragraphs.add(paragraphLine);
}
}catch (IOException e) {
e.printStackTrace();
}
}
private static void writeHTML(String anHTMLString) {
try {
writer.write(anHTMLString);
writer.flush();
}catch(IOException e) {
e.printStackTrace();
}
}
private static void writeParagraphs() {
for(int i=0; i<headings.size();i++) {
writeHTML(HTMLTags.H1_START + headings.get(i)+HTMLTags.H1_END);
writeHTML(HTMLTags.PARAGRAPH_START);
writeHTML(paragraphs.get(i));
writeHTML(HTMLTags.PARAGRAPH_END);
}
}
private static void writeBio(String title) {
writeHTMLOpening(title);
writeParagraphs();
writeHTMLClosing();
}
private static void writeHTMLOpening(String title) {
writeHTML(HTMLTags.HTML_HEADER_START);
writeHTML(HTMLTags.TITLE_START + title + HTMLTags.TITLE_END);
writeHTML(HTMLTags.HTML_HEADER_END);
writeHTML(HTMLTags.BODY_START);
}
private static void writeHTMLClosing() {
writeHTML(HTMLTags.BODY_END);
writeHTML(HTMLTags.HTML_END);
}
public static void main(String[] args) {
initialize(OUTPUT_FILE);
writeBio(TITLE);
readParagraphs(INPUT_FILE);
System.out.println("completed");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>My LIfe</title><style>
h1 {text-align:center;}
</style>
</head>
<body></body></html>
这里是我的bio.txt输入文件
[h1] Who Am I?
My name is Billy. I am a senior CS major.
[h1] What are my interests?
I would like to develop my skillset in software and web development and possibly start a career in it.
[h1] Why am I taking this class?
I am taking this class because I'd like to enhance my web development skills.
[h1] What is my five year plan?
My five year plan is to graduate with my CS major and earn a great job that I enjoy.
答
只是一个小校正方法执行顺序。您需要先阅读并初始化您的标题和段落,然后将其写入html文件。
public static void main(String[] args) {
initialize(OUTPUT_FILE);
readParagraphs(INPUT_FILE);
writeBio(TITLE);
System.out.println("completed");
}
+0
谢谢!这个小小的改正正是我所需要的! –
+0
最好关闭你的'reader'和'writer'。像这样'try {...} catch(IOException ex){...} finally {reader.close}'。 – Matt
你是什么意思的问题是,当我运行它没有输入到文件?你的意思是把数组保存(写入)到一个html文件中,因为在java中的输入意味着从文件中读取并输出意味着从文件中写入 – abcOfJavaAndCPP
我建议只使用新的非阻塞I/O文件,路径和OutputStream,InputStream – abcOfJavaAndCPP
如何使用http://freemarker.org/类模板引擎而不是手动创建HTML。问题虽然没有给你清楚的理解你的用法。 – dwij