如何在Java中替换特定字符串之间的文件内容?

huangapple go评论136阅读模式
英文:

How to replace file content between some specific strings in java?

问题

> 文件_1
>> 第一个文件的内容。想要用第二个文件的内容替换这里的所有内容
>
> 文件_2
>> 第二个文件的内容。
>
> 需要的文件
>> 第一个文件的内容。第二个文件的内容。使用第二个文件的内容。

英文:

I have two files, want to put content of one file into another between some specific start string and end string.

> File_1
>> The first file content. Want to replace all the content here with the contents of second file.
>
> File_2
>> The Second file content.
>
> Required_File
>> The first file content. The Second file content. with the contents of second file.

Assumptions made:
start string and end string will be always unique

答案1

得分: 0

这是操作步骤:

  1. 将文件加载到一个字符串中。
  2. 使用你的字符串,找到起始和结束文本。你可以使用正则表达式或者 String.indexOf 来实现这一步。
  3. 使用 String.replace 来将中间内容替换为你想要的内容。
  4. 将字符串重新写入文件中。
英文:

This is how to do:

  1. Load the file into a String
  2. With your String, find the start and end text. You can use regex or String.indexOf for this
  3. Use String.replace to replace the middle content with the content you want
  4. Write the String back to the file

答案2

得分: 0

我编写了这个快速的模型来给你一个想法

在我编写的演示中1.txt包含我的名字是安德烈。”,而2.txt包含约翰”。我的代码使用了两个变量,`uniqueStart``uniqueEnd`,并使用了字符串的 `indexOf` 函数来提取我们想要的 `1.txt` 的部分然后将 `2.txt` 的内容也就是约翰”,放在中间从而创建最终的字符串 `我的名字是约翰。`

然后我将它保存到一个新文件中只是作为演示

```java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;

class Main {
  public static void main(String[] args) throws IOException{
    
    String firstFileContent = new String(Files.readAllBytes(Paths.get("1.txt")), StandardCharsets.UTF_8);

    String secondFileContent = new String(Files.readAllBytes(Paths.get("2.txt")), StandardCharsets.UTF_8);

    //First file content example: "My name is Andre."
    //Second file content example: "John".
    String uniqueStart = "My name is ";
    String uniqueEnd = ".";
    
    String combined = firstFileContent.substring(0, firstFileContent.indexOf(uniqueStart)+uniqueStart.length()) + secondFileContent + firstFileContent.substring(firstFileContent.indexOf(uniqueEnd), firstFileContent.length());
    //Combined = My name is John.
    
    Files.write(Paths.get("NewContent.txt"), combined.getBytes());
  }
}
英文:

I wrote this quick mockup to give you an idea:

In my demo I wrote, 1.txt contains "My name is Andre.", and 2.txt contains "John". My code uses the two variables, uniqueStart and uniqueEnd, and uses the indexOf function of Strings to substring the parts we want of 1.txt. It then puts the contents of 2.txt, which is "John", in the middle, creating the final String My name is John.

I then save it to a new file just as a demonstration.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
class Main {
public static void main(String[] args) throws IOException{
String firstFileContent = new String(Files.readAllBytes(Paths.get("1.txt")), StandardCharsets.UTF_8);
String secondFileContent = new String(Files.readAllBytes(Paths.get("2.txt")), StandardCharsets.UTF_8);
//First file content example: "My name is Andre."
//Second file content example: "John".
String uniqueStart = "My name is ";
String uniqueEnd = ".";
String combined = firstFileContent.substring(0, firstFileContent.indexOf(uniqueStart)+uniqueStart.length()) + secondFileContent + firstFileContent.substring(firstFileContent.indexOf(uniqueEnd), firstFileContent.length());
//Combined = My name is John.
Files.write(Paths.get("NewContent.txt"), combined.getBytes());
}
}

huangapple
  • 本文由 发表于 2020年6月5日 21:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/62216735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定