Android-Android下怎么遍历assets录下的子文件夹
答案:2 悬赏:50 手机版
解决时间 2021-12-21 22:28
- 提问者网友:我一贱你就笑
- 2021-12-21 06:32
Android-Android下怎么遍历assets录下的子文件夹
最佳答案
- 五星知识达人网友:野慌
- 2021-12-21 07:00
此代码未使用递归 :
import java.io.File;
import java.util.LinkedList;
public class FileSystem {
public static void main(String[] args) {
long a = System.currentTimeMillis();
LinkedList list = new LinkedList();
File dir = new File("c:\\java\\");
File file[] = dir.listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
File tmp;
while (!list.isEmpty()) {
tmp = list.removeFirst();
if (tmp.isDirectory()) {
file = tmp.listFiles();
if (file == null)
continue;
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
} else {
System.out.println(tmp.getAbsolutePath());
}
}
System.out.println(System.currentTimeMillis() - a);
}
}
import java.io.File;
import java.util.LinkedList;
public class FileSystem {
public static void main(String[] args) {
long a = System.currentTimeMillis();
LinkedList list = new LinkedList();
File dir = new File("c:\\java\\");
File file[] = dir.listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
File tmp;
while (!list.isEmpty()) {
tmp = list.removeFirst();
if (tmp.isDirectory()) {
file = tmp.listFiles();
if (file == null)
continue;
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
} else {
System.out.println(tmp.getAbsolutePath());
}
}
System.out.println(System.currentTimeMillis() - a);
}
}
全部回答
- 1楼网友:旧脸谱
- 2021-12-21 07:53
遍历文件夹可以有2中方式: a.不使用递归: import java.io.file; import java.util.linkedlist; public class filesystem { public static void main(string[] args) { long a = system.currenttimemillis(); linkedlist list = new linkedlist(); file dir = new file("c:\\java\\"); file file[] = dir.listfiles(); for (int i = 0; i < file.length; i++) { if (file[i].isdirectory()) list.add(file[i]); else system.out.println(file[i].getabsolutepath()); } file tmp; while (!list.isempty()) { tmp = list.removefirst(); if (tmp.isdirectory()) { file = tmp.listfiles(); if (file == null) continue; for (int i = 0; i < file.length; i++) { if (file[i].isdirectory()) list.add(file[i]); else system.out.println(file[i].getabsolutepath()); } } else { system.out.println(tmp.getabsolutepath()); } } system.out.println(system.currenttimemillis() - a); } } b.使用递归: import java.io.file; import java.util.arraylist; public class filesystem1 { private static arraylist filelist = new arraylist(); public static void main(string[] args) { long a = system.currenttimemillis(); refreshfilelist("c:\\java"); system.out.println(system.currenttimemillis() - a); } public static void refreshfilelist(string strpath) { file dir = new file(strpath); file[] files = dir.listfiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { if (files[i].isdirectory()) { refreshfilelist(files[i].getabsolutepath()); } else { string strfilename = files[i].getabsolutepath().tolowercase(); system.out.println("---"+strfilename); filelist.add(files[i].getabsolutepath()); } } } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯