2

I'm doing my first Android app here and I'm trying to write an ArrayList<String> to a file, and read it back.

Here's my code to write the file:

(Class is called SaveFiles)

public static void writeList(Context c, ArrayList<String> list){
        try{
            FileOutputStream fos = c.openFileOutput("NAME", Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(list);
            os.close();
Log.v("MyApp","File has been written");
        }catch(Exception ex){
            ex.printStackTrace();
Log.v("MyApp","File didn't write");
        }
    }

I don't have code yet to read the file.

Code that calls this method:

//TempArrays.loadCustomTitles() loads a pre-created file that has entities 

    SaveFiles.writeList(getApplicationContext(), TempArrays.loadCustomTitles());

The log says the file's been created, but I can't find the file ANYWHERE on my phone, I literally looked in all places the file should be, so either it's hidden or, more precisely, isn't being created.

I declared

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in my manifest.

Anyone know what's wrong with it?

3
  • Are you testing on emulator or real device? If real device - is it rooted? Commented Feb 27, 2015 at 15:35
  • 1
    Is "NAME" a file? You're not using the external storage directory as the storage location. Commented Feb 27, 2015 at 15:39
  • 2
    stackoverflow.com/questions/12158483/… Commented Feb 27, 2015 at 15:43

2 Answers 2

1

You're unable to see the file because you used openFileOutput() which creates the file on device's internal storage directory. Files created here are private to your application and aren't even visible to the the user. These files are automatically removed if the user uninstalls the application.

To write a file onto the external storage use the getExternalStoragePublicDirectory() method.

// check if sd card is mounted and available for read & write
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    try {
            // create a file in downloads directory
            FileOutputStream fos =
              new FileOutputStream(
                new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_DOWNLOADS), "name.ser")
            );
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(list);
            os.close();
            Log.v("MyApp","File has been written");
    } catch(Exception ex) {
            ex.printStackTrace();
            Log.v("MyApp","File didn't write");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to test it on emulator and use Android Device Monitor to search for the file.

Otherwise, if you want to save file on SD card use File sd = Environment.getExternalStorageDirectory() to get SD card path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.