Roberto Zuniga
Roberto Zuniga

Reputation: 437

How to write new lines (enter) in a text file on Android?

I'm trying to add a new line (what we do with Enter key on PC) in a text file in Android.

This is my code:

try {
    File tarjeta = Environment.getExternalStorageDirectory();
    File archivo = new File(tarjeta.getAbsolutePath()+"/.Info/", "values.txt");
    OutputStreamWriter escritor = new OutputStreamWriter(new FileOutputStream(archivo), "UTF-8");
    escritor.write("Pencil" + "\n" + "Book");
    escritor.flush();
    escritor.close();
    Toast.makeText(this, "Guardado satisfactoriamente!", Toast.LENGTH_SHORT).show();
} catch(IOException ioe) {

}

So everything is ok, BUT when I take this ".txt" file to a Windows OS and open it, I get this result:

PencilBook

While I want this result:

Pencil
Book

Upvotes: 0

Views: 8557

Answers (1)

Lesleh
Lesleh

Reputation: 1675

Windows uses \r\n as its line separator, unlike UNIX which uses just \n. That's why it displays incorrectly in Notepad on Windows.

Upvotes: 6

Related Questions