ChatGPT解决这个技术问题 Extra ChatGPT

Android; Check if file exists without creating a new one

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?

possible duplicate of Test if file exists
@Kunok I'm checking your edit comment: removed words such as tanks as they are... :P
@KevinGuan Oh yeah my bad, just got home from new eve party so I was not able to write properly :)

M
Maikel Bollemeijer

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.

Dont know why in my case this code is creating a new file.
How to check in sub folder also?
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.
I think OP doesn't wish to create new file object.
@AndroDev no - he doesn't wish to create new FILE, answer creates new REFERENCE to file.
V
Victor Laerte

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

J
Jordi Vicens

It worked for me:

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

This is the solution if you only have the file name and not its path
@Zach Not really, it's path would be the first parameter I sent ( getApplicationContext().getFilesDir() )
P
PearsonArtPhoto

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
        }
    }
}

A
Anand Dwivedi

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 ");
 }

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

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

G
Gibolt

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.