The code below loads all lines of the text file into memory, changes characters 8 to 15 of the first line, and saves back to file all lines, including the altered line:
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 guarantees of the current content of the first line (to avoid exceptions of index out of range).
This article might help you Manipulating Java 8 Resources Files
– Nelson Aguiar