Android; Check if file exists without creating a new one

AndroidFileFile Io

Android Problem Overview


I want to check if file exists in my package folder, but I don't want to create a new one.

File file = new File(filePath);
if(file.exists()) 
     return true;

Does this code check without creating a new file?

Android Solutions


Solution 1 - Android

Your chunk of code does not create a new one, it only checks if its already there and nothing else.

File file = new File(filePath);
if(file.exists())      
//Do something
else
// Do something else.


           

Solution 2 - Android

When you use this code, you are not creating a new File, it's just creating an object reference for that file and testing if it exists or not.

File file = new File(filePath);
if(file.exists()) 
    //do something

Solution 3 - Android

It worked for me:

File file = new File(getApplicationContext().getFilesDir(),"whatever.txt");
    if(file.exists()){
       //Do something
    }
    else{
       //Nothing
     }

Solution 4 - Android

When you say "in you package folder," do you mean your local app files? If so you can get a list of them using the Context.fileList() method. Just iterate through and look for your file. That's assuming you saved the original file with Context.openFileOutput().

Sample code (in an Activity):

public void onCreate(...) {
    super.onCreate(...);
    String[] files = fileList();
    for (String file : files) {
        if (file.equals(myFileName)) {
            //file exits
        }
    }
}

Solution 5 - Android

The methods in the Path class are syntactic, meaning that they operate on the Path instance. But eventually you must access the file system to verify that a particular Path exists

 File file = new File("FileName");
 if(file.exists()){
 System.out.println("file is already there");
 }else{
 System.out.println("Not find file ");
 }

Solution 6 - Android

public boolean FileExists(String fname) {
        File file = getBaseContext().getFileStreamPath(fname);
        return file.exists();
}

Solution 7 - Android

if(new File("/sdcard/your_filename.txt").exists())){
              // Your code goes here...
}

Solution 8 - Android

Kotlin Extension Properties

No file will be create when you make a File object, it is only an interface.

To make working with files easier, there is an existing .toFile function on Uri

You can also add an extension property on File and/or Uri, to simplify usage further.

val File?.exists get() = this?.exists() ?: false
val Uri?.exists get() = File(this.toString).exists()

Then just use uri.exists or file.exists to check.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMBHView Question on Stackoverflow
Solution 1 - AndroidMaikel BollemeijerView Answer on Stackoverflow
Solution 2 - AndroidVictor LaerteView Answer on Stackoverflow
Solution 3 - AndroidJordi VicensView Answer on Stackoverflow
Solution 4 - Androidthomas88wpView Answer on Stackoverflow
Solution 5 - AndroidAnand DwivediView Answer on Stackoverflow
Solution 6 - AndroidHomieZ2View Answer on Stackoverflow
Solution 7 - AndroidIsira AdithyaView Answer on Stackoverflow
Solution 8 - AndroidGiboltView Answer on Stackoverflow