//Java遍历文件夹的2种方法-不使用递归1
import java.io.File;
import java.util.LinkedList;
public class FileSystemWithoutRecurisive {
public static void main(String[] args) {
long a = System.currentTimeMillis();
LinkedList list = new LinkedList();
File dir = new File("F:\\bjsxt_msb_hibernate");
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 = (File) 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);
}
}
// 219ms
==================================================
//Java遍历文件夹的2种方法-使用递归
import java.io.File;
import java.util.ArrayList;
public class ListFileSystemUseRecurisive {
private static ArrayList filelist = new ArrayList();
public static void main(String[] args) {
long a = System.currentTimeMillis();
refreshFileList("F:\\bjsxt_msb_hibernate");
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());
}
}
}
}
// 结论:经过测试,使用递归的方法B性能相对好一些。 //78ms
import java.io.File;
import java.util.LinkedList;
public class FileSystemWithoutRecurisive {
public static void main(String[] args) {
long a = System.currentTimeMillis();
LinkedList list = new LinkedList();
File dir = new File("F:\\bjsxt_msb_hibernate");
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 = (File) 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);
}
}
// 219ms
==================================================
//Java遍历文件夹的2种方法-使用递归
import java.io.File;
import java.util.ArrayList;
public class ListFileSystemUseRecurisive {
private static ArrayList filelist = new ArrayList();
public static void main(String[] args) {
long a = System.currentTimeMillis();
refreshFileList("F:\\bjsxt_msb_hibernate");
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());
}
}
}
}
// 结论:经过测试,使用递归的方法B性能相对好一些。 //78ms