码迷,mamicode.com
首页 > 编程语言 > 详细

java遍历文件夹

时间:2014-07-02 19:31:57      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   java   color   使用   

使用递归: 
    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:/Users/shibin/Desktop/tupian");  
            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());                      
                }   
            }   
        }  
    }  
不使用递归:
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:/Users/shibin/Desktop/tupian");
        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);
    }
}

 

java遍历文件夹,布布扣,bubuko.com

java遍历文件夹

标签:des   style   blog   java   color   使用   

原文地址:http://www.cnblogs.com/konghou/p/3819643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!