java – Sending text file from Android


How about friends, I am developing an application that generates a text file ( .txt ) with multiple lines obtained from a DB, and then send through an email, the problem is when the email is received and when viewing the file in a PC, the text comes out in a single line, when seeing 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
  registros = String.format(codigoReloj + c.getString(0).toString() + c.getString(1).toString().replace("/", "") + c.getString(2).toString().replace(":", "")+"\n");
  fileWritter.append( registros );
  registros = "";

This is the code I use to send the file

Intent emailIntent = new Intent(Intent.ACTION_SEND);         
     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 generating the file with:


With these options on the cell phone the line breaks are displayed, after sending it and viewing the file on a PC, the text comes out in a single line.


This seems to be a problem related to the different representation of the new line character, depending on the operating system. According to New Line – Wikipedia (emphasis added):

Different representations of the new line in operating systems sometimes cause that when transferring a file between two computers, it is displayed incorrectly. For example, under normal conditions, files created on Unix or Apple Macintosh systems will appear as one long line on Windows. And vice versa: files created on a Windows computer will look strange with some editors, as the extra CR that Unix doesn't need will show up as a ^ M at the end of each line.

Typically the newline character is represented as:

  • \n = LF ( Line break) Used as a new line character in Unix.
  • \r = CR ( Carriage return ) Used as a new line character in Mac OS.
  • \r\n = LF CR Used as a new line character in Windows and MS-DOS.

When you mention:

With these options, the cell phone displays the line breaks, after sending it and viewing the file on a PC, the text comes out in a single line.

So, assuming that by PC you mean a Windows computer, the newline character you should use is \r\n . In other words:*


* In this small example, registros should not contain \r or \n . It is recommended to add the newline character in a separate call to .append . Additionally, it is also recommended, to improve performance, to flush just before closing the Writer instance.

Scroll to Top