gpt4 book ai didi

java - 将文件中的单词替换为新的文件内容

转载 作者:行者123 更新时间:2023-12-01 10:08:21 24 4
gpt4 key购买 nike

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

我知道如何使用字符串 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 的标签文件数据。

我怎样才能实现这个目标?

最佳答案

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

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

关于java - 将文件中的单词替换为新的文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com