\r\n en FileWriter para la siguiente línea

I am writing into a text file and i am using System.getProperty(line.separator); to go to next line.

FileWriter fw = new FileWriter(file,true);
   fw.write("Correct Fields are:"+System.getProperty("line.separator")+"\r\n");

"\n" was not working before so i searched on the web and found "\r\n".

Qué es "\r\n" ? Is it notepad specific ?

preguntado el 12 de junio de 12 a las 21:06

6 Respuestas

\r is carriage return and \n is line feed.

This format is OS specific. For windows/DOS \r\n is required. For unix \n will be enough and for mac \r is used. (until Mac OS 9).Since Mac OS 10+ , \n is used.

Espero que esto ayude.

Respondido el 13 de junio de 12 a las 23:06

Note that modern Apple computers, using Mac OS X (10+), use linefeed (\n). Older macs, using Mac OS 9 and before, used a carriage return (\r). - maéricos

oops I did not that. Thanks for sharing. - Krishna

No worries, since your answer is accepted as the correct one you should update it with the correct information! Press the "edit" button and mention that \n is now used by Mac OS X. - maéricos

El concepto de a "newline" is handled differently on different systems.

To (over)simplify things: Windows uses CRLF (\r\n), UNIX and most other popular modern systems just use a LF (\n).

Conveniently, or annoyingly, depending on your perspective, Java sometimes converts "newlines" (\n) into the correct form for the target platform.

Respondido el 12 de junio de 12 a las 21:06

"\r\n" is two characters: "Carriage return" and "Line feed" . In Windows, every new line is "\r\n", while on UNIX systems a new line is just "\n".

Respondido el 12 de junio de 12 a las 21:06

The newline sequence is platform dependent. On Windows it is \r\n. On most Unix systems it is \n. On older versions of Mac, \r se utiliza. \r is the carriage return (CR) character and \n is the line feed (LF) character.

Seguí leyendo

Respondido el 12 de junio de 12 a las 21:06

Q: What is "\r\n" ? Is it notepad specific?

A: No, "\r\n" ("Carriage return-Line Feed") is DOS/Windows specific.

Most compilers just alias "\n" ("newline") to the appropriate sequence for the current platform. A DOS/Windows compiler will generate the two characters "\r\n"; a Linux compiler will generate the single character "\n" for the same program.

Java (in the interests of cross-platform compatibility) lets you explicitly state what you want.

Respondido el 12 de junio de 12 a las 21:06

The line separator is platform-dependent (e.g., "\n" en UNIX, "\r\n" para ventanas).

You can save yourself some trouble if instead of using a FileWriter directly you wrap it into a BufferedWriter que tiene un newLine() method, or within PrintWriter que proporciona un println method that deals with these issues for your behind the scenes.

Respondido el 12 de junio de 12 a las 23:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.