英文:
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
这是操作步骤:
- 将文件加载到一个字符串中。
- 使用你的字符串,找到起始和结束文本。你可以使用正则表达式或者 String.indexOf 来实现这一步。
- 使用 String.replace 来将中间内容替换为你想要的内容。
- 将字符串重新写入文件中。
英文:
This is how to do:
- Load the file into a String
- With your String, find the start and end text. You can use regex or String.indexOf for this
- Use String.replace to replace the middle content with the content you want
- 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());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论