In addition to the above answer;
If you do not have a huge file, you can write:
List<String> lines = Files.readAllLines(Paths.get(myfile.txt), StandardCharsets.UTF_8);
lines.add("my string");
Files.write(Paths.get(myfile.txt), lines, StandardCharsets.UTF_8);
It is usually useful to add a new line in the middle of the file.
List<String> lines = Files.readAllLines(Paths.get(myfile.txt)), StandardCharsets.UTF_8);
lines.add(6, "my string");
Files.write(Paths.get(myfile.txt), lines, StandardCharsets.UTF_8);
source
share