检查文件是否存在于android中
我在下面的位置有一个文件检查文件是否存在于android中,android,kotlin,Android,Kotlin,我在下面的位置有一个文件EN.quickdic 如果我检查文件是否存在->它总是返回FALSE val dirPath: String = context.filesDir.absolutePath val filePath = dirPath.plus("/$Constants.DICTIONARY_FILE") // - > /data/user/0/com.cnx.dictionarytool/files/EN.quickdic val file =
EN.quickdic
如果我检查文件是否存在->它总是返回FALSE
val dirPath: String = context.filesDir.absolutePath
val filePath = dirPath.plus("/$Constants.DICTIONARY_FILE")
// - > /data/user/0/com.cnx.dictionarytool/files/EN.quickdic
val file = File(filePath)
// Check if file does not exists
if (file.exists()) {
// -------------------- > This is still not true even if file is present
}
问题:如何检查文件是否存在尝试检查文件是否存在,如下所示
public static boolean fileExists(File directory, String fileName)
{
File file = new File(directory,fileName);
if(file.exists())
{
return true;
}
else{
return false;
}
}
尝试检查文件是否存在,如下所示
public static boolean fileExists(File directory, String fileName)
{
File file = new File(directory,fileName);
if(file.exists())
{
return true;
}
else{
return false;
}
}
- 我有可用的运行时权限
- 我已在清单中声明权限
fun isDictionaryExists(): Boolean {
val isFileFound = false
val externalStorageVolumes: Array<out File> = ContextCompat.getExternalFilesDirs(context, null)
for (item: File in externalStorageVolumes) {
val primaryExternalStorage = item
Log.d("","")
return traverseForCheckingTheFile(item)
}
return isFileFound
}
private fun traverseForCheckingTheFile(dir: File): Boolean {
if (dir.exists()) {
val files = dir.listFiles()
for (i in files.indices) {
val file = files[i]
if (file.isDirectory) {
Log.d("","")
traverseForCheckingTheFile(file)
} else {
Log.d("","")
if(file.endsWith(DICTIONARY_FILE)){
//Dictionary file found
return true
}
}
}
}
return false
}
fun isDictionaryExists():布尔值{
val isFileFound=false
val externalStorageVolumes:Array=ContextCompat.getExternalFilesDirs(上下文,null)
用于(项目:externalStorageVolumes中的文件){
val primaryExternalStorage=项目
Log.d(“,”)
返回遍历或检查文件(项目)
}
返回isFileFound
}
private-fun-traverseForCheckingTheFile(dir:File):布尔值{
if(dir.exists()){
val files=dir.listFiles()
对于(i在files.index中){
val file=files[i]
if(file.isDirectory){
Log.d(“,”)
遍历或检查文件(文件)
}否则{
Log.d(“,”)
if(file.endsWith(DICTIONARY_文件)){
//找到字典文件
返回真值
}
}
}
}
返回错误
}
我从字典中调用
isDictionaryExists()
- 我有可用的运行时权限
- 我已在清单中声明权限
fun isDictionaryExists(): Boolean {
val isFileFound = false
val externalStorageVolumes: Array<out File> = ContextCompat.getExternalFilesDirs(context, null)
for (item: File in externalStorageVolumes) {
val primaryExternalStorage = item
Log.d("","")
return traverseForCheckingTheFile(item)
}
return isFileFound
}
private fun traverseForCheckingTheFile(dir: File): Boolean {
if (dir.exists()) {
val files = dir.listFiles()
for (i in files.indices) {
val file = files[i]
if (file.isDirectory) {
Log.d("","")
traverseForCheckingTheFile(file)
} else {
Log.d("","")
if(file.endsWith(DICTIONARY_FILE)){
//Dictionary file found
return true
}
}
}
}
return false
}
fun isDictionaryExists():布尔值{
val isFileFound=false
val externalStorageVolumes:Array=ContextCompat.getExternalFilesDirs(上下文,null)
用于(项目:externalStorageVolumes中的文件){
val primaryExternalStorage=项目
Log.d(“,”)
返回遍历或检查文件(项目)
}
返回isFileFound
}
private-fun-traverseForCheckingTheFile(dir:File):布尔值{
if(dir.exists()){
val files=dir.listFiles()
对于(i在files.index中){
val file=files[i]
if(file.isDirectory){
Log.d(“,”)
遍历或检查文件(文件)
}否则{
Log.d(“,”)
if(file.endsWith(DICTIONARY_文件)){
//找到字典文件
返回真值
}
}
}
}
返回错误
}
从我调用的字典
isDictionaryExists()
可以看到,您只是使用了错误的文件路径。val dirPath:String=getExternalFilesDir(null)!!。absolutePath,我对此感到厌倦,file.exist是真的。正如您所看到的,您只是使用了错误的文件路径。val dirPath:String=getExternalFilesDir(null)!!。绝对路径,我厌倦了这个,file.exist是真的