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

这是我的文件夹结构有时我需要检查文件是否存在于男性中有时我需要检查文件是否存在于女性中我该怎么办。

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

这只给出了我不能进去检查存在的男人和女人的名单。

他们还有其他方法可以检查吗

4

2 回答 2

0

使用以下方法从资产文件夹中获取文件。我提供代码获取所有 .mp4 文件并将文件存储到字符串列表中。

private List<String> videoList=new ArrayList<>();
  private boolean listAssetFiles(String path) {

    String [] list;
    try {
        list = getAssets().list(path);
        if (list.length > 0) {
            // This is a folder
            for (String file : list) {
                if (!listAssetFiles(path + "/" + file))
                    return false;
                else {
                    // This is a file
                    // TODO: add file name to an array list
                    if (file.contains(".mp4") || file.contains(".mp3") ) {
                        videoList.add(file);
                    }
                }
            }
        }
    } catch (IOException e) {
        return false;
    }

    return true;
}

当您调用此方法时,仅传递..

    listAssetFiles(""); // you can pass your path if is empty then it scan root.
于 2018-04-12T14:52:39.413 回答
0

只需打开文件的输入流。就好像你要阅读文件一样。

如果您设法打开它,该文件确实存在。否则你会得到一个例外。

于 2018-04-12T18:03:52.893 回答