How about friends, I am developing an application that generates a text file ( .txt
) with multiple lines obtained from a DB, to then send through an email, the problem is when the email is received and when viewing the file on a PC, the text appears in a single line, when viewing the same file on the cell phone if it has the line break.
This is the code I use to concatenate and save to a text file:
BufferedWriter fileWritter = new BufferedWriter(new FileWriter("/sdcard/arhivos/"+currentDate)); //ubicacion y nombre del archivo
while(c.moveToNext()){
registros = String.format(codigoReloj + c.getString(0).toString() + c.getString(1).toString().replace("/", "") + c.getString(2).toString().replace(":", "")+"\n");
fileWritter.append( registros );
fileWritter.flush();
registros = "";
}
fileWritter.close();
This is the code I use to send the file
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_CC, cc);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, asunto);
emailIntent.putExtra(Intent.EXTRA_TEXT, mensaje);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"/storage/sdcard0/archivos/"+ asunto));
emailIntent.setType("text/plain"); //indicamos el tipo de dato
startActivity(Intent.createChooser(emailIntent, "Email "));
I already tried to generate the file with:
writer.newLine();
System.getProperty("line.separator");
String.format("%n");
With these options, the line breaks are displayed on the cell phone, after sending it and viewing the file on a PC, the text appears in a single line.
This seems to be a problem related to the different representation of the newline character , depending on the operating system. According to Newline - Wikipedia (emphasis added):
Typically the new line character is represented as:
\n
=LF
( Line feed ) Used as a newline character in Unix.\r
=CR
( Carriage return ) Used as a new line character in Mac OS.\r\n
=LF
CR
Used as a newline character in Windows and MS-DOS.When you mention:
So, assuming that by PC you mean a Windows computer, the newline character to use is
\r\n
. In other words:*────────
* In this small example,
registros
it must not contain\r
or\n
. It is recommended that you add the newline character in a separate call to.append
. Additionally, it is also recommended, to improve performance, to doflush
just before closing the instance ofWriter
.You can try to use or employ something similar to the following, it may be what you are looking for:
.
My recommendation is that you always save in the windows line break format since windows to linux is supported , but not if you save in the linux format.
With this you will avoid going from one data transformation to another, simply always save in Windows format.
Anyway with this code you can convert Linux Lines to Windows Lines
Departure:
You can try it online here
line break <br>
If you want to add line breaks in the text of your email in the case of Android (since I see you use an Intent), the html tag that produces a line break and carriage-return is regularly used . Instead of\n
using<br>
, which you can also add to the end of each line, add it when calling .append() :So when sending the email:
and receive it in the client, your message will be formed with line breaks and not in a single line, this image shows how the text would look on a mobile device or on a pc.
* It is important to note that some email clients filter html content.