title: Android - How can I write an ArrayList to a text file line by line and read it back?
body: <p>I'm very new to Android/Java programming (have some experience in VB and took some C++ classes in college many years ago but don't remember anything), so I apologize for the headache in advance.</p>
  
  <p>I'm using Android Studio and I've worked on this for a couple days now (lots of Google searches) and can't get anything to function the way I want it to.  I can write to a text file, can read from the text file but can't get it to write line by line, read line by line or even count how many lines are there to begin with (my initial thought is if I can count how many lines I could do a loop to write/read by line), but I'm just missing it.</p>
  
  <p>I know some of the code is sloppy, I'm learning from pulled examples from all over the web.  Currently, it'll create the ArrayList items, display it in the first ListView and write it to a text file, and I can read it back into the second ListView but it's in a single line format that looks like:</p>
  
  <pre><code>[Testing - how are you?, I'm good and you?, Great, thanks!]
  </code></pre>
  
  <p>Examples on writing/reading line by line that I've found on the web either don't work or rely on 3rd party dependencies which I'd rather not use and figure out how to do it myself.  Thank you in advance!</p>
  
  <pre><code>package com.example.thedoctor.readwritearraylists;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.util.ArrayList;
  
  public class MainActivity extends AppCompatActivity {
      ArrayList&lt;String&gt; origitems;
      ArrayList&lt;String&gt; newitems;
  
      ArrayAdapter&lt;String&gt; origitemsAdapter;
      ArrayAdapter&lt;String&gt; newitemsAdapter;
  
      ListView lvOrigItems;
      ListView lvNewItems;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          lvOrigItems = (ListView) findViewById(R.id.lvOriginal);
          lvNewItems = (ListView) findViewById(R.id.lvNew);
  
          origitems = new ArrayList&lt;String&gt;();
          newitems = new ArrayList&lt;String&gt;();
  
          origitemsAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, origitems);
          newitemsAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, newitems);
  
          lvOrigItems.setAdapter(origitemsAdapter);
          lvNewItems.setAdapter(newitemsAdapter);
  
      }
  
      public void makeArray(View v) {
          origitemsAdapter.add("Testing - how are you?");
          origitemsAdapter.add("I'm good and you?");
          origitemsAdapter.add("Great, thanks!");
          origitemsAdapter.notifyDataSetChanged();
      }
  
      public void saveItems(View v) {
          String file = "myfile.txt";
          String huh = String.valueOf(origitems);
          try {
              FileOutputStream fileout = openFileOutput(file, MODE_PRIVATE);
              OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
              outputWriter.write(String.valueOf(huh) + System.getProperty("line.separator"));
              outputWriter.close();
  
              //display file saved message
              Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void readItems(View v) {
          String file = "myfile.txt";
          int READ_BLOCK_SIZE = 100;
  
          try {
              FileInputStream fileIn = openFileInput(file);
              InputStreamReader InputRead = new InputStreamReader(fileIn);
              char[] inputBuffer = new char[READ_BLOCK_SIZE];
              String s = "";
              int charRead;
  
              while ((charRead = InputRead.read(inputBuffer)) &gt; 0) {
                  // char to string conversion
                  String readstring = String.copyValueOf(inputBuffer, 0, charRead);
                  s += readstring;
              }
              newitemsAdapter.add(s);
              newitemsAdapter.notifyDataSetChanged();
              InputRead.close();
              Toast.makeText(getBaseContext(), "The file " + file + " has been read from internal storage\n", Toast.LENGTH_SHORT).show();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>