Check if file exists on Android SD card

I want to check if a text file exists on the SD card. The file name is mytextfile.txt. Below is the code:

FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE); 

How to check if this file exists?

+9
android
Oct 08 '11 at 3:01 a.m.
source share
6 answers

This should do the trick, I replaced the hardcode link SDcard with the recommended API call getExternalCacheDir () :

 File file = new File(getExternalCacheDir(), "mytextfile.txt" ); if (file.exists()) { //Do action } 
+51
Oct 08 '11 at 15:07
source share

See this file System in android: Working with the SDCards file system in Android

you just check

 if(file.exists()){ // } 
+2
Oct 08 '11 at 15:05
source share

* With this you can check whether a file exists or not in sdcard *

 File file = new File(sdcardpath+ "/" + filename); if (file.exists()) { } 
+2
Jun 13 '13 at 10:53 on
source share

You need to create a file and install it as the necessary file and check if it exists.

 String FILENAME="mytextfile.txt"; File fileToCheck = new File(getExternalCacheDirectory(), FILENAME); if (fileToCheck.exists()) { FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE); } 

Good luck

0
Oct 08 '11 at 15:08
source share

The FileOutputStream constructor FileOutputStream exception if the specified file does not exist. See the Oracle documentation for more information.

Also, make sure you have permission to access the SD card .

0
Oct 08 2018-11-11T00:
source share

check if condition with boolean type

 File file = new File(path+filename); if (file.exists() == true) { //Do something } 
0
May 23 '13 at 7:11
source share



All Articles