循环遍历某个文件夹下面的所有文件,递归方法

	public List traverseFolder(List fileList,String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return fileList;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder(fileList,file2.getAbsolutePath());
                    } else {
                    	fileList.add(file2);
                        System.out.println("文件:" + file2.getAbsolutePath());
                    }
                }
                return fileList;
            }
        } else {
        	return fileList;
        }
    }

你可能感兴趣的:(Java后台)