Let's start by understanding what a Line Jump means English Wikipedia about "new line" or if you can not understand well English Wikipedia in Spanish on "new line" (the link in Spanish that is very poor at which I would like to use google translate in the English version ...)
In synthesis there are several ways to demonstrate a new line depending on the application and / or Operating System. The most common are:
Unix like: '\n'
Windows & DOS: '\r'+'\n'
'\r'
- Macs (OS 9 and earlier)
where '\n'
means "Line Feed" and '\r'
Carriage return
With the above clear, remember that Java is independent of the platform and can use or not the same character. in elements such as the JTextArea the default line breaks are represented as '\n'
so when you use editor.getText()
this returns in String. that most likely their lines are separated by '\n'
to save the String to a file and keep the "new lines" with the same format of the Operating System where the application is executed must be made one of The following actions:
A:
String salto = System.lineSeparator(); //java 6 para atras se utiliza: System.getProperty("line.separator");
String towrite= editor.getText().replace("\n",salto);
writer.println(towrite);
writer.flush();
writer.close();
B:
String salto = System.lineSeparator(); //java 6 para atras se utiliza: System.getProperty("line.separator");
editor.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, salto);
editor.write(writer);
writer.flush();
writer.close();
more about this method see this link
particularly in your code the problem you have is the following:
new String()
is redundant And besides an innesesario memory expense the Strings in java are Immutable!
String archivo=new String(rutas.getText());
must be
String archivo=rutas.getText();
this line is over and / or the variable is badly named
String archivo2=new String(editor.getText());//aqui se optiene el texto que tiene el editor
modify it to something more significant:
String texto_editor= editor.getText();
And the biggest offensor! saltos
is the text that replaced '\n'
by "\r\n"
but nothing is done with the because the next line what it does is save file2 that is done with variable saltos
String saltos = editor.getText().replace("\n", "\r\n");
writer.println(archivo2);
...
}
Remember, the Strings are immutable. if you read a text of the UI and modify it, this is not reflected in the UI, you must re enter the text, and even if that were the case archivo2
no is a reference to the text of editor
therefore the same is wrong.