简体   繁体   中英

How to know the file is exist in SD Card or Internal Storage in Android

How can I come to know that any file is exist or not in Android sdcard or internal storage?

Case : we don't have exact location directory. I want to check that file in any directory.

If there is any method or any example to do so please let me know.

Thank you

You can check whether File exists or not by using File.exists()

     File file = new File(filePathString); 
        if(file.exists()) {     
        //File is present
        }
        else{
        // File is not present,Create a new one.
       try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }

Please follow below samples available in stackoverflow

Sample 1

Sample 2

I think above links may help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM