Saving string array list to internal file directory android

605 views Asked by At

I'm currently developing an android application with makes some user data while running which is saved in two ArrayList. Once the application closes, I need to save the datas on to the internal memory but in my logcat I'm always getting the error IOException file not found.

I'm confused about why it always happen. The following is my code for my activity. Please help me!

    import java.io.BufferedReader;
    import java.io.DataInputStream;
     import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;
    import android.text.Html;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends ActionBarActivity {


public static ArrayList<String> myArrayList=new ArrayList<String>();
public static ArrayList<String> myArrayListwr=new ArrayList<String>();
static int close=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    close=1;

     System.out.println("Oncreate called");

    try{ 
     final File dir = new File(getApplicationContext().getFilesDir() + "/file");

        if(!(dir.exists()))
        {

             dir.mkdirs(); //create folders where write files

        }
         final File file = new File(dir+ "/lines.txt");


             if(!(file.exists())) {

                 file.createNewFile();

                } 

             BufferedReader br = new BufferedReader(new FileReader(file));     
                if (br.readLine() == null) {
                    close=0;
                }
                br.close();

    }
    catch (IOException exc) { exc.printStackTrace(); } 




     try {
         if(myArrayList.size()==0 && close!=0)
         {
             System.out.println("retriving data from file lines");


             FileInputStream input =openFileInput("lines.txt");  // Open input stream

         DataInputStream din = new DataInputStream(input);

         int sz = din.readInt(); // Read line count
         for (int i=0;i<sz;i++) { // Read lines
            String line = din.readUTF();
            myArrayList.add(line);
         }
         din.close();
         }
        }
        catch (IOException exc) { exc.printStackTrace(); } 

     try {
         if(myArrayListwr.size()==0 && close!=0)
         {
             System.out.println("retriving data from file lineswr");
             final File dirwr = new File(getApplicationContext().getFilesDir() + "/file");
             if(!dirwr.exists())
             dirwr.mkdirs(); //create folders where write files
             final File filewr = new File(dirwr+ "/lineswr.txt");
             if(!filewr.exists()) {
                 filewr.createNewFile();
            } 
         FileInputStream inputwr = openFileInput("lineswr.txt"); // Open input stream
         DataInputStream dinwr = new DataInputStream(inputwr);
         int szwr = dinwr.readInt(); // Read line count
         for (int iwr=0;iwr<szwr;iwr++) { // Read lines
            String linewr = dinwr.readUTF();
            myArrayListwr.add(linewr);
         }
         dinwr.close();
         }
        }
        catch (IOException exc) { exc.printStackTrace(); } 

    Button phy = (Button) findViewById(R.id.button3);

     Button mat = (Button) findViewById(R.id.button1);

     Button bio = (Button) findViewById(R.id.button2);

     Button chem = (Button) findViewById(R.id.button4);

     }



@Override
public void onBackPressed() {

    super.onBackPressed();


    try {
        if(myArrayList.size()!=0)
        {
            System.out.println("inside on back pressed saving data to lines");
           //Modes: MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE
            String fileName = getApplicationContext().getFilesDir()+"/file/"+ "lines.txt";

           FileOutputStream output = openFileOutput("lines.txt",MODE_PRIVATE);
           DataOutputStream dout = new DataOutputStream(output);
           dout.writeInt(myArrayList.size()); // Save line count
           for(String line : myArrayList) // Save lines
              dout.writeUTF(line);
           dout.flush(); // Flush stream ...
           dout.close(); // ... and close.
        }
    }
        catch (IOException exc) { exc.printStackTrace(); }
    try {
        if(myArrayListwr.size()!=0)
        {
            System.out.println("inside on back pressed saving data to lineswr");
           //Modes: MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE
            String fileNamewr = getApplicationContext().getFilesDir()+"/file/"+ "lineswr.txt";

           FileOutputStream outputwr = openFileOutput("lineswr.txt",MODE_PRIVATE);
           DataOutputStream doutwr = new DataOutputStream(outputwr);
           doutwr.writeInt(myArrayListwr.size()); // Save line count
           for(String linewr : myArrayListwr) // Save lines
              doutwr.writeUTF(linewr);
           doutwr.flush(); // Flush stream ...
           doutwr.close(); // ... and close.
        }
    }
        catch (IOException exc) { exc.printStackTrace(); }

    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);

  }

}
0

There are 0 answers