原文链接:
java获取指定文件夹下的所有文件名_tomorrowzm的专栏-CSDN博客_java查询指定文件夹下的所有文件输出文件名 site:blog.csdn.netblog.csdn.netpackage Test;
import java.io.File;
/**
* @author yinxm
* @version 1.0 2005/06/17
*
* This class can take file's path and file's name;
* you must give the path where you want to take the file.
*/
public class TakeFilePathAndName {
public static void main(String[] args) {
// This is the path where the file's name you want to take.
String path = "C://Documents and Settings//yinxm//デスクトップ//TestFile";
getFile(path);
}
private static void getFile(String path){
// get file list where the path has
File file = new File(path);
// get the folder list
File[] array = file.listFiles();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
System.out.println("^^^^^" + array[i].getName());
// take file path and name
System.out.println("#####" + array[i]);
// take file path and name
System.out.println("*****" + array[i].getPath());
}else if(array[i].isDirectory()){
getFile(array[i].getPath());
}
}
}
}
这里我们主要使用的是listFiles函数来得到file文件夹下的所有文件,包括文件夹。
然后通过File类的isFile和isDirectory来区分,如果是文件,就输出对应的信息,如果是目录,就迭代进行这个函数。