如何使用Java用新行替换行
我使用缓冲区读取器解析整个文件。如果找到Oranges:模式,我想用AppleSandRanges替换它如何使用Java用新行替换行,java,bufferedreader,filewriter,Java,Bufferedreader,Filewriter,我使用缓冲区读取器解析整个文件。如果找到Oranges:模式,我想用AppleSandRanges替换它 try (BufferedReader br = new BufferedReader(new FileReader(resourcesFilePath))) { String line; while ((line = br.readLine()) != null) { if (line.startsWith("Oranges:")){
try (BufferedReader br = new BufferedReader(new FileReader(resourcesFilePath))) {
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("Oranges:")){
int startIndex = line.indexOf(":");
line = line.substring(startIndex + 2);
String updatedLine = "ApplesAndOranges";
updateLine(line, updatedLine);
我调用一个updateLine方法,传递我的原始行和更新的行值
private static void updateLine(String toUpdate, String updated) throws IOException {
BufferedReader file = new BufferedReader(new FileReader(resourcesFilePath));
PrintWriter writer = new PrintWriter(new File(resourcesFilePath+".out"), "UTF-8");
String line;
while ((line = file.readLine()) != null)
{
line = line.replace(toUpdate, updated);
writer.println(line);
}
file.close();
if (writer.checkError())
throw new IOException("Can't Write To File"+ resourcesFilePath);
writer.close();
}
要更新文件,我必须使用其他名称(resourcesFilePath+“.out”)保存它。如果使用原始文件名,保存的版本将变为空白。所以我的问题是,如何用原始文件中的任何值替换一行而不丢失任何数据 为此,您需要使用如下正则表达式(RegExp): str=str.replaceAll(“^Orange:(.*)”,“OrangeAndApples:$1”) 这是一个例子,也许它不是你想要的,但在这里,在第一个参数中,parentesis中的表达式被称为捕获组。找到的表达式将替换为第二个参数,$1将替换为捕获组的值。在我们的示例中,行行距处的橙色:Hello将被橙色替换Dapples:Hello 在代码中,每行创建一个文件。。。也许内联子方法会更好
try (
BufferedReader br = new BufferedReader(new FileReader(resourcesFilePath));
BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset);
) {
String line;
while ((line = br.readLine()) != null) {
String repl = line.replaceAll("Orange:(.*)","OrangeAndApples:$1");
writer.writeln(repl);
}
}
为此,您需要使用如下正则表达式(RegExp): str=str.replaceAll(“^Orange:(.*)”,“OrangeAndApples:$1”) 这是一个例子,也许它不是你想要的,但在这里,在第一个参数中,parentesis中的表达式被称为捕获组。找到的表达式将替换为第二个参数,$1将替换为捕获组的值。在我们的示例中,行行距处的橙色:Hello将被橙色替换Dapples:Hello 在代码中,每行创建一个文件。。。也许内联子方法会更好
try (
BufferedReader br = new BufferedReader(new FileReader(resourcesFilePath));
BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset);
) {
String line;
while ((line = br.readLine()) != null) {
String repl = line.replaceAll("Orange:(.*)","OrangeAndApples:$1");
writer.writeln(repl);
}
}
在你的原始期末考试中,写下所有内容最简单的方法就是阅读所有内容——改变你想改变的内容并关闭流程。然后再次打开文件,然后用您想要的数据覆盖文件及其所有行。在原始期末考试中重写所有内容的最简单方法是读入所有内容-更改您想要更改的内容并关闭流。然后再次打开文件,然后用所需数据覆盖文件及其所有行。您可以使用RandomAccessFile写入文件,使用nio.Files读取其中的字节。在这种情况下,我把它作为一个字符串 您也可以使用RandomAccessFile读取文件,但在我看来,这样做更容易
import java.io.RandomAccessFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
public void replace(File file){
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
Path p = Paths.get(file.toURI());
String line = new String(Files.readAllBytes(p));
if(line.startsWith("Oranges:")){
line.replaceAll("Oranges:", "ApplesandOranges:");
raf.writeUTF(line);
}
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
您可以使用RandomAccessFile写入该文件,使用nio.Files从中读取字节。在这种情况下,我把它作为一个字符串 您也可以使用RandomAccessFile读取文件,但在我看来,这样做更容易
import java.io.RandomAccessFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
public void replace(File file){
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
Path p = Paths.get(file.toURI());
String line = new String(Files.readAllBytes(p));
if(line.startsWith("Oranges:")){
line.replaceAll("Oranges:", "ApplesandOranges:");
raf.writeUTF(line);
}
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
读取每一行,处理它,将它写入一个新文件。完成后,删除旧文件并将新文件重命名到它的位置。您需要使用新行重新写入文件。您不能使用line.ReplaceProgrammer进行输入。我使用了一个数组来重新写入数据。我不确定这是否是一个过度杀戮,因为我只需要替换资源文件中的一行。请将您的评论作为答案发布,以便相应地接受。你能确认在不重新写入整个文件的情况下,没有办法替换那一行吗?读取每一行,处理它,将它写入一个新文件。完成后,删除旧文件并将新文件重命名到它的位置。您需要使用新行重新写入文件。您不能使用line.ReplaceProgrammer进行输入。我使用了一个数组来重新写入数据。我不确定这是否是一个过度杀戮,因为我只需要替换资源文件中的一行。请将您的评论作为答案发布,以便相应地接受。你能确认在不重新写入整个文件的情况下,没有办法替换那一行吗?谢谢你的输入!但我的目标是用新数据覆盖原始文件。在您的示例中,将数据写入outputfilePath。我想把它写到resourcesFilePath。这似乎可以解决问题,但如果我试图覆盖原始文件,该文件将变为空白。感谢您的输入!但我的目标是用新数据覆盖原始文件。在您的示例中,将数据写入outputfilePath。我想把它写到resourcesFilePath。这似乎起到了作用,但如果我试图覆盖原始文件,该文件将变为空白。