Search code examples
androidfilefile-exists

Android file exists problems


When i make File file = new File(etc..) i actuly created that file on SD card? Iam bit confused because everytime my condition is true and program jumps in if tree but in that time there is no file on SD card...

    String filename = "pictures.data";
    String root = Environment.getExternalStorageDirectory().toString();
    File dir = new File(root + "/courier/saved/");
    File file = new File(dir,filename);

    if (file.exists())
        // program jumps here
    else{

    }

Solution

  • OK so you can see the file in a dir /courier/saved/. Anyway here's my answer

    String filename = "pictures.data";
    String root = Environment.getExternalStorageDirectory().toString();
    File dir = new File(root + "/courier/saved/");
    File file = new File(dir,filename);
    file.mkdirs();
    if (file.exists())
        // program now stops here
    else{
    
    }
    

    Best would be to create the dir beforehand maybe, But this creates it on the fly