用新文件内容替换文件中的单词

时间:2016-03-30 14:46:21

标签: java

我正在编写一个代码,其中文件中的数据必须替换为其他文件内容。

我知道如何使用字符串Replace()函数。但问题是,我想用一个全新的数据替换一个字符串。

我可以追加(private static void writeDataofFootnotes(File temp, File fout))内容,但无法知道如何替换它。

以下是我的代码。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;

public class BottomContent {

    public static void main(String[] args) throws Exception {

        String input = "C:/Users/u0138039/Desktop/Proview/TEST/Test/src.html";
        String fileName = input.substring(input.lastIndexOf("/") + 1);
        URL url = new URL("file:///" + input);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        File fout = new File("C:/Users/u0138039/Desktop/TEST/Test/OP/" + fileName);
        File temp = new File("C:/Users/u0138039/Desktop/TEST/Test/OP/temp.txt");

        if (!fout.exists()) {
            fout.createNewFile();
        }
        if (!temp.exists()) {
            temp.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(fout);
        FileOutputStream tempOs = new FileOutputStream(temp);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        BufferedWriter tempWriter = new BufferedWriter(new OutputStreamWriter(tempOs));
        String inputLine;
        String footContent = null;
        int i = 0;
        while ((inputLine = in.readLine()) != null) {

            if (inputLine.contains("class=\"para\" id=\"")) {
                footContent = inputLine.replaceAll(
                        "<p class=\"para\" id=\"(.*)_(.*)\" style=\"text-indent: (.*)%;\"><a href=\".*\">(.*)</a>(.)(.*)</p>",
                        "<div class=\"tr_footnote\">\n<div class=\"footnote\">\n<sup><a name=\"ftn.$2\" href=\"#f$2\" class=\"tr_ftn\">$4</a></sup>\n"
                                + "<div class=\"para\">" + "$6" + "\n</div>\n</div>\n</div>");
                inputLine = inputLine.replaceAll(
                        "<p class=\"para\" id=\"(.*)_(.*)\" style=\"text-indent: (.*)%;\"><a href=\".*\">(.*)</a>(.)(.*)</p>",
                        "");
                tempWriter.write(footContent);
                tempWriter.newLine();
            }
            inputLine = inputLine.replace("</body>", "<hr/></body>");

            bw.write(inputLine);
            bw.newLine();

        }

        tempWriter.close();
        bw.close();
        in.close();
        writeDataofFootnotes(temp, fout);

    }

    private static void writeDataofFootnotes(File temp, File fout) throws IOException {

        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(temp);
            fw = new FileWriter(fout, true);
            int c = fr.read();
            while (c != -1) {
                fw.write(c);
                c = fr.read();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fr);
            close(fw);
        }

    }

    public static void close(Closeable stream) {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            // ...
        }
    }
}

这里我正在搜索特定字符串并将其保存在单独的txt文件中。一旦我完成了这项工作。我想将<hr />标记替换为整个txt文件数据。

请让我知道我该怎么做。

由于

1 个答案:

答案 0 :(得分:1)

我按如下方式修改您的处理循环:

while ((inputLine = in.readLine()) != null) {

    // Stop translation when we reach end of document.
    if (inputLine.contains("</body>") {
        break;
    }

    if (inputLine.contains("class=\"para\" id=\"")) {
        // No changes in this block
    }

    bw.write(inputLine);
    bw.newLine();
}

// Close temporary file
tempWriter.close();

// Open temporary file, and copy verbatim to output
BufferedReader temp_in = Files.newBufferedReader(temp.toPath());
String footnotes;
while ((footnotes = temp_in.readLine()) != null) {
    bw.write(footnotes);
    bw.newLine();
}
temp_in.close();

// Finish document
bw.write(inputLine);
bw.newLine();

while ((inputLine = in.readLine()) != null) {
    bw.write(inputLine);
    bw.newLine();
}

// ... and close all open files