我正在用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文件中,因爲在java中的輸入意味着從文件中讀取並輸出意味着從文件中寫入 – abcOfJavaAndCPP
我建議只使用新的非阻塞I/O文件,路徑和OutputStream,InputStream – abcOfJavaAndCPP
如何使用http://freemarker.org/類模板引擎而不是手動創建HTML。問題雖然沒有給你清楚的理解你的用法。 – dwij