Java中遍历文件夹的2种方法
|
〖 作者:月影传说 〗〖 大小:2k 〗〖 发布日期:2008-03-27 〗〖 浏览:1 〗
Java遍历文件夹的2种方法: A.不使用递归: import java.io.File; |
<script type="text/javascript">
</script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1832179689702023&dt=1221022839823&lmt=1206589756&format=160x600_as&output=html&correlator=1221022839823&url=http%3A%2F%2Fwww.java3z.com%2Fcwbwebhome%2Farticle%2Farticle5%2F5924.html%3Fid%3D1838&color_bg=FFFFFF&color_text=000000&color_link=3366FF&color_url=3D81EE&color_border=FFFFFF&ad_type=text_image&ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3Djava%2B%25B1%25E9%25C0%25FA%25CE%25C4%25BC%25FE%25BC%25D0%26lm%3D0%26si%3D%26rn%3D10%26ie%3Dgb2312%26ct%3D0%26cl%3D3%26f%3D1%26rsp%3D4&frm=0&cc=72&ga_vid=2535835193166384600.1221022840&ga_sid=1221022840&ga_hid=111241095&flash=0&u_h=1024&u_w=1280&u_ah=994&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=14&u_nmime=55" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" width="160" frameborder="0" height="600">
|
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());
}
}
}
}
结论:经过测试,使用递归的方法B性能相对好一些。