You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何检查Assets子文件夹中HTML文件是否存在?求替代方案

如何检查Android Assets子目录中的文件是否存在?

问题背景

我的Assets文件夹结构如下:

Assets
|-man
|  |-myhtml.html
|-women
|  |-myhtml.html

我需要检查manwomen子文件夹中的myhtml.html是否存在,但尝试的代码只能获取根目录下的manwomen列表,无法进入子目录检查文件:

try { 
    fileExist = Arrays.asList(getResources().getAssets().list("")).contains(pathInAssets); 
} catch (FileNotFoundException e) { 
    fileExist = false; 
} catch (IOException e) { 
    e.printStackTrace(); 
}

请问有没有可行的解决方法?


解决方案

你的问题核心在于getResources().getAssets().list("")仅返回Assets根目录的条目,无法直接访问子目录内容。这里有两种针对性的解决方法:

方法一:直接指定子目录检查(适合简单二级目录)

如果你的目录结构只有两级(根目录→子目录→文件),可以直接传入子目录路径到list()方法,获取该子目录下的所有文件后再判断目标文件是否存在:

public boolean isAssetFileExist(String filePath) {
    // 拆分路径,比如把"man/myhtml.html"拆成子目录和文件名
    String[] pathParts = filePath.split("/");
    // 处理根目录文件的情况
    if (pathParts.length == 1) {
        try {
            return Arrays.asList(getResources().getAssets().list("")).contains(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    String subDir = pathParts[0];
    String targetFileName = pathParts[1];
    try {
        // 获取子目录下的所有文件/文件夹
        String[] filesInSubDir = getResources().getAssets().list(subDir);
        return Arrays.asList(filesInSubDir).contains(targetFileName);
    } catch (FileNotFoundException e) {
        // 子目录不存在,文件肯定也不存在
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

调用示例:isAssetFileExist("man/myhtml.html")isAssetFileExist("women/myhtml.html"),直接返回文件是否存在的布尔值。

方法二:递归遍历Assets目录(支持任意深度嵌套)

如果你的Assets目录有更深的层级(比如man/subdir1/subdir2/file.html),可以写一个递归方法遍历所有目录,直到找到目标文件:

public boolean isAssetFileExistRecursive(String targetFullPath) {
    return checkAssetDirRecursively("", targetFullPath);
}

private boolean checkAssetDirRecursively(String currentDirectory, String targetFullPath) {
    try {
        String[] entries = getResources().getAssets().list(currentDirectory);
        for (String entry : entries) {
            // 拼接当前条目的完整路径
            String fullEntryPath = currentDirectory.isEmpty() ? entry : currentDirectory + "/" + entry;
            // 找到目标文件,直接返回true
            if (fullEntryPath.equals(targetFullPath)) {
                return true;
            }
            // 尝试进入子目录继续查找
            try {
                // 如果是目录,list()会返回其内容,不会抛出FileNotFoundException
                if (getResources().getAssets().list(fullEntryPath).length > 0) {
                    if (checkAssetDirRecursively(fullEntryPath, targetFullPath)) {
                        return true;
                    }
                }
            } catch (FileNotFoundException e) {
                // 当前条目是文件,不是目录,跳过递归
                continue;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 遍历完所有目录都没找到,返回false
    return false;
}

这个方法可以适配任何复杂的Assets目录结构,调用时直接传入完整路径即可,比如isAssetFileExistRecursive("man/subdir/myhtml.html")

注意事项

  • Assets中的路径是区分大小写的,确保你传入的路径和实际文件路径的大小写完全一致;
  • 如果你的App需要频繁检查文件存在性,建议提前缓存Assets的目录结构,避免重复调用list()方法影响性能。

内容的提问来源于stack exchange,提问作者Yadu Mohan

火山引擎 最新活动