На всякий случай, если кто-то захочет прочитать текст с включенным '\n'
.
попробуйте простой подход
Итак,
Скажем, у вас есть три строки данных (скажем, в файле .txt
), например
This is the data to write in buffer!
This is the second line
This is the third line
И , читая , вы делаете что-то вроде этого
String content=null;
String str=null;
while((str=bufferedReader.readLine())!=null){ //assuming you have
content.append(str); //your bufferedReader declared.
}
bufferedReader.close();
System.out.println(content);
и ожидая, что на выходе будет
This is the data to write in buffer!
This is the second line
This is the third line
но почесать голову, увидев вывод в виде одной строки
This is the data to write in buffer!This is the second lineThis is the third line
Вот что вы можете сделать
добавив этот фрагмент кода в цикл while
if(str.trim().length()==0){
content.append("\n");
}
Итак, как должна выглядеть ваша while
петля
while((str=bufferedReader.readLine())!=null){
if(str.trim().length()==0){
content.append("\n");
}
content.append(str);
}
Теперь вы получите требуемый вывод (в виде трех строк текста)
This is the data to write in buffer!
This is the second line
This is the third line