列出目录下所有文件包括子目录的文件路径-采用递归方式

/**
* 列出目录下所有文件包括子目录的文件路径
* @param dirName
* 文件夹的文件路径
*/
public static void imageScan(String dirName
) {

// 如果dir不以文件分隔符结尾,自动添加文件分隔符。
if (!dirName.endsWith(File.separator)) {
dirName = dirName + File.separator;
}
File dirFile = new File(dirName);
// 如果dir对应的文件不存在,或者不是一个文件夹,则退出
if (!dirFile.exists() || (!dirFile.isDirectory())) {
System.out.println("List失败!找不到目录:" + dirName);
return;
}
// 列出源文件夹下所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].canRead()) // 能读
{
// 开始
if (files[i].isFile()) {

//System.out.println(files[i].getAbsolutePath() + " 是文件!");
String filename = files[i].getName();
String fullpath = files[i].getPath();
String code=getFileName(filename);

String format=getExitName(files[i].getName());


long filesize=files[i].length();
String dirname=files[i].getParent();
System.out.println("path:"+files[i].getAbsolutePath());
System.out.println("format:"+format);
//System.out.println("filename:"+code);



} else if (files[i].isDirectory()) {
ImageScan.imageScan(files[i].getAbsolutePath());
}
// 结束

} else // 不能读
{

}

// /////////////////////////

}
}

// /////////////////
public static String getpostfix(String fname) {
String postfix = null;
if (fname == null)
return "";
if (fname.indexOf(".") != -1) {
postfix = fname.substring(fname.indexOf("."));
} else {
return "非法文件名";
}
return postfix;
}

public static String getFileName(String fname) {
String postfix = null;
if (fname == null)
return "";
if (fname.indexOf(".") != -1) {
postfix = fname.substring(0,fname.indexOf("."));
} else {
return "非法文件名";
}
return postfix;
}

public static String getExitName(String fname) {
String postfix = null;
if (fname == null)
return "";
if (fname.indexOf(".") != -1) {
postfix = fname.substring(fname.indexOf("."));
} else {
return "非法文件名";
}
return postfix;
}

你可能感兴趣的:(文件)