我想检查在安卓SD卡中是否存在给定的文件。我尝试使用绝对路径创建一个文件,并使用file.exists()
进行检查,但是它没有起作用。文件的URL为"file:///mnt/sdcard/book1/page2.html"
,并且该文件确实存在。但是,一些原因导致file.exists()
未显示相同的结果。
我想检查在安卓SD卡中是否存在给定的文件。我尝试使用绝对路径创建一个文件,并使用file.exists()
进行检查,但是它没有起作用。文件的URL为"file:///mnt/sdcard/book1/page2.html"
,并且该文件确实存在。但是,一些原因导致file.exists()
未显示相同的结果。
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.html");
if(myFile.exists()){
...
}
这应该可以工作。
尝试像这样:
File file = new File(Environment.getExternalStorageDirectory() + "/book1/page2.html");
if (file.exists()) {
/*...*/
}
同时确保您已经具备以下条件:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
File file = new File(getExternalCacheDirectory(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
File file = new File(path+filename);
if (file.exists())
{
//Do something
}
已检查,这将正常工作
String filepath = getFilesDir().getAbsolutePath();
String FileName = "Yourfilename" ;
File FileMain = new File(filepath, FileName);
if (FileMain.exists()){
do somthing here
}else{}
File logFile = new File(
android.os.Environment.getExternalStorageDirectory()
+ "/book1/", "page2.tml");
if (logFile.exists())
System.out.println("file exists");
else
System.out.println("file does not exist
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your/file/path");
if(yourFile.exists())
{
}