¿Cómo obtengo un carácter de nueva línea dependiente de la plataforma?


¿Cómo obtengo una nueva línea dependiente de la plataforma en Java? No puedo usar "\n" en todas partes.

Author: Rafael Winterhalter, 2008-10-16

9 answers

Además De la línea.propiedad separator, si está utilizando java 1.5 o posterior y la cadena .formatear (u otros métodos formatear) puede usar %n como en

Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c); 
//Note `%n` at end of line                                  ^^

String s2 = String.format("Use %%n as a platform independent newline.%n"); 
//         %% becomes %        ^^
//                                        and `%n` becomes newline   ^^

Vea la API Java 1.8 para Formatter para más detalles.

 347
Author: Alex B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-05 22:28:26

Puede usar

System.getProperty("line.separator");

Para obtener el separador de líneas

 631
Author: abahgat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-20 07:59:19

Java 7 ahora tiene un System.lineSeparator() método.

 627
Author: StriplingWarrior,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-12 20:00:03

Si estás intentando escribir una nueva línea en un archivo, simplemente puedes usar el método de BufferedWriter newLine().

 42
Author: Michael Myers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-16 18:49:15

Esto también es posible: String.format("%n").

O String.format("%n").intern() para guardar algunos bytes.

 28
Author: ceving,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-06 10:50:18

La biblioteca commons-lang tiene un campo constante disponible llamado SystemUtils.LINE_SEPARATOR

 22
Author: lexicalscope,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-03 19:42:03

Si está escribiendo en un archivo, usando una instancia BufferedWriter, use el método newLine() de esa instancia. Proporciona una forma independiente de la plataforma para escribir la nueva línea en un archivo.

 12
Author: Damaji kalunge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-23 09:48:19
StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();

El fragmento anterior tendrá dos cadenas separadas por una nueva línea independientemente de las plataformas.

 9
Author: Sathesh Balakrishnan Manohar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-29 11:20:31

Evite agregar cadenas usando String + String etc, use StringBuilder en su lugar.

String separator = System.getProperty( "line.separator" );
StringBuilder lines = new StringBuilder( line1 );
lines.append( separator );
lines.append( line2 );
lines.append( separator );
String result = lines.toString( );
 -2
Author: Gary Davies,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-03 17:43:23