The question is possibly incorrect, but the solution was not found: For example, there is such a file:
{ "xxxxxxxx": "present/xxxxxxxxxxxxx", "xxxxxxx": { "xxxxxxx": "present/xxxxxxxxxxxx", "xxxxxxx": "present/xxxxxxxxxx" } }
The task is to replace the word "present" with "test: present", and to do this exactly as many times as necessary. That is, if you need 2 times, the result should be like this:
{ "xxxxxxxx": "test:present/xxxxxxxxxxxxx", "xxxxxxx": { "xxxxxxx": "test:present/xxxxxxxxxxxx", "xxxxxxx": "present/xxxxxxxxxx" } }
Initially, neither the file size nor the number of the required lines is known. I tried using String :: replaceFirst in a loop, but in two passes it will change the same string, since one contains the other: present
-> test:present
-> test:test:present
. At the moment my code looks like this:
String writable = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); for(int z = 0; z < stepOnFile; ++z) { writable = writable.replaceFirst("present", "test:present"); }
stepOnFile is just the number of repetitions.