Stornu2
Stornu2

Reputation: 2332

Android File.exists() is always true

I'm trying to copy files from the assets folder to the device folder using this function:

public static void copyJSON(Context aContext) {
    AssetManager assetManager = aContext.getResources().getAssets();
    String[] pFiles = null;
    try {
        pFiles = assetManager.list("ConfigurationFiles");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    if (pFiles != null) for (String pJsonFileName : pFiles) {
        InputStream tIn = null;
        OutputStream tOut = null;
        try {
            tIn = assetManager.open("ConfigurationFiles" + File.separator + pJsonFileName);
            String[] pList = aContext.getFilesDir().list(); //just for test
            File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
            tOut = new FileOutputStream(pOutFile);
            if (pOutFile.exists()) {
                copyFile(tIn, tOut);
            }
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + pJsonFileName, e);
        } finally {
            if (tIn != null) {
                try {
                    tIn.close();
                } catch (IOException e) {
                    Log.e("tag", "Fail closing", e);
                }
            }
            if (tOut != null) {
                try {
                    tOut.close();
                } catch (IOException e) {
                    Log.e("tag", "Fail closing", e);
                }
            }
        }
    }
}

If I delete the App and run the code, the variable pList is empty as I expect but the pOutFile.exists()returns true ALWAYS!!.

I don't want to copy them again every time I open my App, and I'm doing this because all my app uses JSON to navigate thru all the screens, so If I change any value in my BBDD a WS send a new JSON file and the App respond in accordance for example a button is no longer needed, so the first time you download my App I copy the original JSON and then if you use the app an if you have internet connection you will download a new JSON file that it is more accurate than the one that is in the Bundle and it will be override, this is because as far as I know I can't change the files that are in the assets folder.

I have read everywhere and all say the same use this:

File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);

And then ask for this:

pOutFile.exists()

I don't know what I'm doing wrong.

Thanks for all your help.

Upvotes: 0

Views: 939

Answers (3)

user207421
user207421

Reputation: 310913

A file that you have just created without getting an exception always exists. The test is pointless. Remove it.

Upvotes: 0

jste89
jste89

Reputation: 446

The problem is you're essentially creating a file and then checking if it exists.

try {
    tIn = assetManager.open("ConfigurationFiles" + File.separator + pJsonFileName);
    String[] pList = aContext.getFilesDir().list(); //just for test
    File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);

    // See here: you're creating a file right here
    tOut = new FileOutputStream(pOutFile);

    // And that file will be created in the exact location of the file
    // you're trying to check:
    if (pOutFile.exists()) {  // Will always be true if FileOutputStream was successful
        copyFile(tIn, tOut);
    }
}

You should instead create your FileOutputStream AFTER you've done your existence check.

Source: http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html

Upvotes: 0

ScreamingTree
ScreamingTree

Reputation: 312

put it this way:

File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
if (pOutFile.exists()) {
  tOut = new FileOutputStream(pOutFile);
  copyFile(tIn, tOut);
}

and everything should work fine. Remember the FileOutputStream creates the file it should stream to if possible and non existing

Upvotes: 1

Related Questions