本文介绍了查找在SD卡上的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想找到存储在SD卡中的所有文件。我用这个code:
I want to find all files stored on sdCard. I used this code:
List<File> dir = new ArrayList<File>();
List<File> files = new ArrayList<File>();
File root = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
scan(root); // find root directories
for (File f : dir) { // find in root...
scan(f);
}
Log.i("files", "" + files.size());
Log.i("dir", "" + dir.size());
}
public void scan (File path) {
for (File f : path.listFiles()) {
if (f.isFile()) {
files.add(f);
}
else {
dir.add(f);
}
}
}
但我有问题,与此异常:java.util.ConcurrentModificationException更改列表(DIR)和环路它。该的ListIterator没有帮助。
But I have problem with this exception: "java.util.ConcurrentModificationException"I change list (dir) and loop it. The ListIterator doesn't help.
推荐答案
您不能添加到列表中你遍历它一段时间。试试这个:
You can't add to a list while you're iterating over it. Try this instead:
List<File> dir = new ArrayList<File>();
List<File> files = new ArrayList<File>();
File root = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
List<File> rootDirs = //... code to find root directories
for (File f : rootDirs) { // find in root...
scan(f);
}
Log.i("files", "" + files.size());
Log.i("dir", "" + dir.size());
}
public void scan (File path) {
for (File f : path.listFiles()) {
if (f.isFile()) {
files.add(f);
}
else {
dir.add(f);
scan(f);
}
}
}
这篇关于查找在SD卡上的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!