如何得到某一个文件夹下带某个特定后缀名的文件总数,注意这个文件夹下可以再套文件夹,再套的文件夹还可以套文件夹

package com.cloudwalk.ibis.model.system;

import org.springframework.util.Assert;

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

/**
* @author qinlinsen
*/
public class FileTest {
private static final String FILE_NAME = "D:\\cloudwalk\\IBIS3.0\\src\\main\\resources\\cn\\cloudwalk\\mapper";

public static void countFileNumber(String directoryPath, String fileExtensiveName) {
ArrayList<File> filesWithSpecialExtensiveName = new ArrayList<File>();
ArrayList<File> filesWithoutSpecialExtensiveName = new ArrayList<File>();
Assert.notNull(directoryPath, "file is required");
Assert.notNull(fileExtensiveName, "file extensive name is required");
File file = new File(directoryPath);
Assert.state(file.isDirectory(), "This require is directory rather than file,please check you input");
//get all files of this directory
File[] files = file.listFiles();
iteratorDirectory(fileExtensiveName, filesWithSpecialExtensiveName, filesWithoutSpecialExtensiveName, files);
System.out.println("the total number of file with " + fileExtensiveName
+ " is " + filesWithSpecialExtensiveName.size() +
" without the " + fileExtensiveName + " the count number is : "
+ filesWithoutSpecialExtensiveName.size());
}

private static void iteratorDirectory(String fileExtensiveName, ArrayList<File> filesWithSpecialExtensiveName, ArrayList<File> filesWithoutSpecialExtensiveName, File[] files) {
for (File f : files) {
if(f.isDirectory()){
iteratorDirectory(fileExtensiveName,filesWithSpecialExtensiveName,filesWithoutSpecialExtensiveName,f.listFiles());
}
if (f.isFile()) {
//only ends with special extensive name can add to filesWithSpecialExtensiveName
String fileName = f.getName();
if (fileName.endsWith(fileExtensiveName)) {
filesWithSpecialExtensiveName.add(f);
} else {
filesWithoutSpecialExtensiveName.add(f);
}
}
}
}


public static void main(String[] args) {

countFileNumber("D:\\cloudwalk\\IBIS3.0\\src\\main\\resources\\com\\timo",".xml");

}
}
posted @ 2018-10-24 15:29  技术让世界更精彩  阅读(306)  评论(0编辑  收藏  举报