S
S
shepka6542016-01-08 21:43:14
Java
shepka654, 2016-01-08 21:43:14

How to edit text files in java line by line?

The essence of the question is how to edit a text file located in resources line by line (for example, delete a certain line or update it, write it or read it). Thanks in advance for your answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dorofeev, 2016-01-08
@shepka654

List lines = Files.readAllLines(Paths.get("res/file.txt"));
//перебираете циклом
for(String s : lines)
{
s = "Бла-бла-бла!"
}
/* Если нужно удалить - для перебора используйте Iterator.
Не забудьте : */ 
Files.write(Paths.get("res/file.txt"), lines);
//иначе результат не запишется

With FileWriter and BufferedWriter also.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question