Example-
aa
bbb
ccc
jjj
Output-
zzzzzz <- replaced aa by zzzzzz
bbb
ccc
jjj
No, there is no "insert-mode" in which you can squeeze in new bytes in the middle of the file.
You'll have to read through and "regenerate" the entire file with the characters inserted along the way:
File input = new File("data.txt");
File tmp = File.createTempFile("tmp", null);
String search = "aa";
String replacement = "zzzzzz";
PrintWriter pw = new PrintWriter(tmp);
BufferedReader br = new BufferedReader(new FileReader(input));
String line;
while ((line = br.readLine()) != null) {
if (line.equals(search))
pw.println(replacement);
else
pw.println(line);
}
br.close();
pw.close();
input.delete();
tmp.renameTo(input);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.