Java中遍历文件夹、获取文件内容

阅读更多
package demo.test;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

/**
 * Created by GQ on 6/11 0011.
 */
public class ParseFile {
    public static String path = "E:\\TEMP";
    //private static ArrayList filelist = new ArrayList();

    public static void main(String[] args) throws Exception {
        long a = System.currentTimeMillis();
        refreshFileList(path);
        System.out.println("花费时间:" + (System.currentTimeMillis() - a) / 1000 + "秒");
    }

    public static void refreshFileList(String strPath) throws Exception {
        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());

                File file = new File(strFileName);
                FileReader reader = new FileReader(file);
                int fileLen = (int) file.length();
                char[] chars = new char[fileLen];
                reader.read(chars);
                String txt = String.valueOf(chars);
                System.out.println("--文件内容--" + txt);
            }
        }
    }
}

 

你可能感兴趣的:(java,遍历)