Check if file exists on SD card on Android

后端 未结 6 679
悲哀的现实
悲哀的现实 2020-12-05 20:57

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/myte         


        
6条回答
  •  没有蜡笔的小新
    2020-12-05 21:14

    This should do the trick, I've replaced the hard coded SDcard reference to the recommended API call getExternalCacheDir():

    File file = new File(getExternalCacheDir(), "mytextfile.txt" );
    if (file.exists()) {
      //Do action
    }
    

提交回复
热议问题