Check if file exists on SD card on Android

后端 未结 6 666
悲哀的现实
悲哀的现实 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:05

    check IF condition with Boolean type like

    File file = new File(path+filename);
    if (file.exists() == true)
    {
    //Do something
    }
    
    0 讨论(0)
  • 2020-12-05 21:11

    See this file System in android : Working with SDCard’s filesystem in Android

    you just check

    if(file.exists()){
    //
    }
    
    0 讨论(0)
  • 2020-12-05 21:11

    The FileOutputStream constructor will throw a FileNotFound exception if the specified file doesn't exist. See the Oracle documentation for more information.

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

    0 讨论(0)
  • 2020-12-05 21:13

    *Using this you can check the file is present or not in sdcard *

    File file = new File(sdcardpath+ "/" + filename);
    if (file.exists())
     {
    
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-05 21:29

    You have to create a file, and set it to be the required 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);
    }
    

    Have Fun!

    0 讨论(0)
提交回复
热议问题