title: Reading and writing a serialized object in android
body: <p>I've been searching on here but can't seem to find what works.  I'm trying to write a simple object list to a serialized file and can't seem to locate the file in android.  It's my first time building an app and I had this working in netbeans by pointing to src/list.ser, however, this doesn't work in android studio.  I guess my question is where do I place the ser file and how do I point to it in the code?  Here is my code:</p>
  
  <p>ListActivity:</p>
  
  <pre><code> public class ListActivity extends Activity implements Serializable {
  
      private ArrayList&lt;Item&gt; list;
  
      public List() throws Exception {
          list = new ArrayList&lt;Item&gt;();
      }
  
      public void addItem(String name) {
          list.add(new Item(name));
  
      }
  
      public void addCurrentList() throws Exception{
  
          String pathToAppFolder = getExternalFilesDir(null).getAbsolutePath();
          String filePath = pathToAppFolder +File.separator + "list.ser";
  
          try {
              ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(filePath));
  
  
  
              os.writeObject(list);
              os.close();
          }
          catch (Exception e) {
  
              System.out.println("NOPE NOPE NOPE NOPE");
  
  
          }
      }
  
  
  
      public void addItem(String name, int price) {
          list.add(new Item(name, price));
      }
  
      public ArrayList&lt;Item&gt; populate() {
          return list;
      }
      public void record() {
  
          try {
  
              ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/list.ser"));
              ArrayList&lt;Item&gt; list2 = (ArrayList&lt;Item&gt;) in.readObject();
              System.out.println(list2);
              list = list2;
  
              in.close();
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
  
      }
  
  }
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code> public class MainActivity extends AppCompatActivity {
  
  
  
      private List tester;
      private ListView lv;
      private EditText gg;
      private Button button;
      private Button clearButton;
      private ArrayList list;
      String pathToAppFolder = getExternalFilesDir(null).getAbsolutePath();
      String filePath = pathToAppFolder + File.separator + "list.ser";
  
      @Override
      protected void onDestroy() {
  
          try {
              tester.addCurrentList();
          } catch (Exception e) {
              e.printStackTrace();
          }
          super.onDestroy();
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //create the list
  
          lv = (ListView) findViewById(R.id.listDisplay);
          ListView mine = lv;
          list = new ArrayList&lt;String&gt;();
  
          try {
              tester = new List();
          }
          catch (Exception e) {
  
          }
  
  
  
  
          for (Item item : tester.populate()) {
              list.add(item);
          }
  
          final ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1,list);
  
          lv.setAdapter(arrayAdapter);
  
          final TextView firstTextView = (TextView) findViewById(R.id.textView);
          button = (Button) findViewById(R.id.button);
          clearButton = (Button) findViewById(R.id.clear);
  
          gg = (EditText) findViewById(R.id.item);
  
          button.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  String no = gg.getText().toString();
                  if (!no.isEmpty()) {
                      tester.addItem(no);
                      arrayAdapter.add(no);
                      arrayAdapter.notifyDataSetChanged();
                      gg.setText("");
  
                  }
  
              }
          });
  
          clearButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  arrayAdapter.clear();
                  arrayAdapter.notifyDataSetChanged();
              }
          });
  
  
          lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  String item = lv.getItemAtPosition(position).toString();
  
                  arrayAdapter.remove(arrayAdapter.getItem(position));
                  arrayAdapter.notifyDataSetChanged();
              }
          });
  
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
  }
  </code></pre>