256

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?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
MBH
  • 16,271
  • 19
  • 99
  • 149
  • possible duplicate of [Test if file exists](http://stackoverflow.com/questions/2786655/test-if-file-exists) – piokuc Apr 26 '13 at 13:42
  • 1
    @Kunok I'm checking your edit comment: *removed words such as **tanks** as they are...* :P – Remi Guan Jan 01 '16 at 10:28
  • 2
    @KevinGuan Oh yeah my bad, just got home from new eve party so I was not able to write properly :) – Kunok Jan 01 '16 at 12:38

8 Answers8

489

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.
Maikel Bollemeijer
  • 6,545
  • 5
  • 25
  • 48
  • 6
    Dont know why in my case this code is creating a new file. – ofnowhere Feb 19 '14 at 20:54
  • How to check in sub folder also? – Pratik Butani Apr 26 '14 at 12:55
  • 4
    This is like that because there is no static method : File.exists(String file) , so you have to instanciate a new File object to access 'Exists' method. – Giova Oct 13 '14 at 03:12
  • 3
    I think OP doesn't wish to create new file object. – AndroDev Jul 25 '16 at 14:45
  • 1
    @AndroDev no - he doesn't wish to create new FILE, answer creates new REFERENCE to file. – Marian Paździoch Apr 11 '20 at 17:57
  • [AmirGolan](https://stackoverflow.com/users/11878751/amir-golan) [comments](https://stackoverflow.com/a/62218537/3744182): *when you give the file path, don't forget to include file name at the end of the path. It sounds stupid, but it took me two days to notice I forgot it.* – dbc Jun 05 '20 at 16:43
34

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
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
29

It worked for me:

File file = new File(getApplicationContext().getFilesDir(),"whatever.txt");
    if(file.exists()){
       //Do something
    }
    else{
       //Nothing
     }
Jordi Vicens
  • 696
  • 7
  • 17
10

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
        }
    }
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
thomas88wp
  • 2,381
  • 19
  • 31
5

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 ");
 }
Anand Dwivedi
  • 1,452
  • 13
  • 23
2
public boolean FileExists(String fname) {
        File file = getBaseContext().getFileStreamPath(fname);
        return file.exists();
}
Sunil
  • 3,404
  • 10
  • 23
  • 31
HomieZ2
  • 31
  • 1
2
if(new File("/sdcard/your_filename.txt").exists())){
              // Your code goes here...
}
Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
Isira Adithya
  • 307
  • 2
  • 8
0

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.

Gibolt
  • 42,564
  • 15
  • 187
  • 127