我无法运行每次运行时创建html模板的程序

时间:2017-09-12 01:28:07

标签: java html web

我正在用java编写一个程序,每次运行时都会创建一个HTML模板。它读取输入的bio txt文件,该文件由四个标题和每个段落一行组成。它采用txt并将标题和段落存储为并行数组。然后我将数组写入HTML文件。问题是,当我运行它时,没有输入文件,它也说读者永远不会关闭。这是我的程序和输出。感谢您的任何建议。

 delegate :count, to: :task, prefix: "total"

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

    }
}

这是我的bio.txt输入文件

<!DOCTYPE html> 
<html>
<head>
<meta charset="ISO-8859-1">
<title>My LIfe</title><style> 
h1 {text-align:center;} 
</style> 
</head>  
<body></body></html>

1 个答案:

答案 0 :(得分:0)

按方法执行顺序进行小幅修正。您需要首先阅读并初始化标题和段落,然后将其写入html文件。

public static void main(String[] args) {
    initialize(OUTPUT_FILE);
    readParagraphs(INPUT_FILE);
    writeBio(TITLE);
    System.out.println("completed");
}