Como escrever um novo caractere de linha em um arquivo em Java
Eu tenho uma string que contém novas linhas. Eu envio essa string para uma função para gravar a String em um arquivo de texto como:
public static void writeResult(String writeFileName, String text)
{
try
{
FileWriter fileWriter = new FileWriter(writeFileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(text);
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println("Error writing to file '"+ writeFileName + "'");}
} //end writeResult function
Mas quando eu abro o arquivo, eu o encontro sem nenhuma nova linha. Quando exibo o texto na tela do console, ele é exibido com novas linhas. Como posso escrever o novo caractere de linha no arquivo de texto.
EDITAR: Suponha que este seja o argumentotext
que enviei para a função acima:
I returned from the city about three o'clock on that
may afternoon pretty well disgusted with life.
I had been three months in the old country, and was
Como escrever essa string como é (com novas linhas) no arquivo de texto. Minha função escreve a string em uma linha. Você pode me fornecer uma maneira de escrever o texto no arquivo, incluindo novas linhas?
EDIT 2: O texto está originalmente em um arquivo .txt. Eu li o texto usando:
while((line = bufferedReader.readLine()) != null)
{
sb.append(line); //append the lines to the string
sb.append('\n'); //append new line
} //end while
Ondesb
é um StringBuffer