Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new line on the text file in android

Tags:

android

I am writing the data on the file and i am using ArrayList for it but my main problem is that how can i add the new line on the text file. Is it possible to do it.

In a such a way that when first data of ArrayList write on the text file than automatically in the new line next data of ArrayList should be write

 final String FILES = "/MY_FILE_FOLDER";

 String path= Environment.getExternalStorageDirectory().getPath()+FILES; // Folder path

 File folderFile = new File(path);
  if (!folderFile.exists()) {
    folderFile.mkdirs();
   }

 File myFile = new File(folderFile, fileName+".doc");
 myFile.createNewFile();

 FileOutputStream fOut = new FileOutputStream(myFile);
 OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

 for (int i = 0; i < getdata.size(); i++) {
    myOutWriter.append(getdata.get(i)); 
 }


 myOutWriter.close();
 fOut.close();
 Toast.makeText(getBaseContext(),
        "Done writing SD 'mysdfile.txt'",
        Toast.LENGTH_SHORT).show();
like image 397
Ravindra Kushwaha Avatar asked Nov 21 '14 06:11

Ravindra Kushwaha


People also ask

How do I add a line break in Android?

Add Line Breaks to a TextView Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

How do I add a new line to text?

To start a new line of text or add spacing between lines or paragraphs of text in a worksheet cell, press Alt+Enter to insert a line break.

How do I create a new line in Kotlin?

\n - Inserts newline.


2 Answers

Change,

for (int i = 0; i < getdata.size(); i++) {
    myOutWriter.append(getdata.get(i)); 
}

to

for (int i = 0; i < getdata.size(); i++) {
    myOutWriter.append(getdata.get(i)); 
    myOutWriter.append("\n\r");
}

I Hope this helps!!

like image 54
codePG Avatar answered Nov 02 '22 01:11

codePG


Try in this way,

FileOutputStream fOut = new FileOutputStream(myFile);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fOut));

  for (int i = 0; i < getdata.size(); i++) {
    bw.write(getdata.get(i));
    bw.newLine();
  }

bw.close();

fOut.close();
like image 45
kiran boghra Avatar answered Nov 02 '22 01:11

kiran boghra