Question

I use File f = new File(s) to create a new file from the press of a button, and it saves the file. The 'saved' files are then collected using .fileList() / getApplicationContext().fileList() (both work) and are used in a for loop to save to an ArrayList, which then gets passed to a ListAdapter for a ListView. In the adapter I set a button to delete each file. I placed Logs on each section of the code and when I press a button, the individual buttons and files are recognised, but at f.exists() it always returns null. Even when the list is created using collected files, so the files must be there, it always returns null. Why is this? I've been trying to debug for 3 days now!

code from the adapter to delete (everything else in the adapter works):

public void onClick(View v) {
    try {
        File f = new File(getChild(groupPosition, childPosition).toString());
        if (f.getAbsoluteFile().exists()) {
            f.delete();
            Log.d("delete", f.getAbsolutePath().toString() + " is deleted!");
        } else {
            Log.d("delete", f.getAbsolutePath().toString() + ": doesn't exist?");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Edit: Tried to delete all files in data directory (ones created by = new File(s))

String [] filenames = getApplicationContext().fileList();
if (filenames.length > 0) {
    for (int i = 0; i < filenames.length; i++) {
        File f = new File(filenames[i]);
        Log.d("file selected:", f.getName());
        Log.d("file path:", f.getAbsolutePath().toString());
        if (f.exists()) {
            if (f.delete()) {
                Log.d("deleted:", f.getName());
            } else {
                Log.d("couldn't delete:", f.getName());
            }
        } else {
            Log.d("doesn't exist:", f.getName());
        }
    }
}

Makes this in stack trace: (+10 others)

12-16 21:31:11.187: D/file selected:(1270): new_recipe
12-16 21:31:11.187: D/file path:(1270): /new_recipe
12-16 21:31:11.187: D/doesn't exist:(1270): new_recipe
12-16 21:31:11.197: D/file selected:(1270): Untitled_Recipe
12-16 21:31:11.197: D/file path:(1270): /Untitled_Recipe
12-16 21:31:11.197: D/doesn't exist:(1270): Untitled_Recipe
12-16 21:31:11.197: D/file selected:(1270): hgk
12-16 21:31:11.197: D/file path:(1270): /hgk
12-16 21:31:11.197: D/doesn't exist:(1270): hgk

AAARGH!

Was it helpful?

Solution

Reason of this behaviour is, that File is just wrapper around path String and returns information on demand, like exists(). So when you create new File with name "not existing", getName() etc. will still work.

Nothing is really created on file system when you call new File("fileName"),

only object in memory and then you can call file.createNewFile() to create empty file on specified location.

If you are getting files via getApplicationContext().fileList() these files are in your application private storage and there were created with getApplicationContext().openFileOutput("fileName", mode), their real path is /data/data/<your package name>/files/<filename>.For these file no File object is needed. to create, modify or delete. Everything should be handle through Context and file name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top