The code below loads all lines of the text file into memory, changes the characters 8 to 15 of the first line, and saves all the lines, including the changed line, back to file:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("c:/temp/arquivo.txt");
List<String> linhas = Files.readAllLines(path);
String novoConteudo =
linhas.get(0).substring(0, 7) + "conteudo" + linhas.get(0).substring(15);
linhas.remove(0);
linhas.add(0, novoConteudo);
Files.write(path, linhas);
}
}
* You may have to consider more aspects like performance, encoding and warranties of the current content of the first line (to avoid exceptions from index out of range ). p>