一、试题描述
获取指定目录(例如"E:/test")下的所有文件名
二、Java程序
import java.io.File;
import java.util.ArrayList;
public class DirectoryFileName {
private static ArrayList<String> filelist = new ArrayList<String>();
public static void main(String[] args) throws Exception
{
String pathname = "E:/Datasets/Ukbench100";
getFiles( pathname );
}
// 通过递归得到某一路径下所有的目录及其文件
static void getFiles( String pathname )
{
File root = new File( pathname );
File[] files = root.listFiles();
System.out.println(pathname+"目录下的所有文件名为:");
for (File file : files)
{
if ( file.isDirectory() )
{
// 递归调用
getFiles( file.getAbsolutePath() );
filelist.add( file.getAbsolutePath() );
System.out.println( file.getAbsolutePath() );
}else{
System.out.println(file.getAbsolutePath() );
} //else
} //for
} //getFiles()
}
三、执行结果
E:/Datasets/Ukbench100目录下的所有文件名为:
E:\Datasets\Ukbench100\ukbench00000.jpg
E:\Datasets\Ukbench100\ukbench00001.jpg
E:\Datasets\Ukbench100\ukbench00002.jpg
E:\Datasets\Ukbench100\ukbench00003.jpg
E:\Datasets\Ukbench100\ukbench00004.jpg
E:\Datasets\Ukbench100\ukbench00005.jpg
E:\Datasets\Ukbench100\ukbench00006.jpg
E:\Datasets\Ukbench100\ukbench00007.jpg
E:\Datasets\Ukbench100\ukbench00008.jpg
E:\Datasets\Ukbench100\ukbench00009.jpg
E:\Datasets\Ukbench100\ukbench00010.jpg
E:\Datasets\Ukbench100\ukbench00011.jpg
E:\Datasets\Ukbench100\ukbench00012.jpg
E:\Datasets\Ukbench100\ukbench00013.jpg
E:\Datasets\Ukbench100\ukbench00014.jpg
E:\Datasets\Ukbench100\ukbench00015.jpg
E:\Datasets\Ukbench100\ukbench00016.jpg
E:\Datasets\Ukbench100\ukbench00017.jpg
E:\Datasets\Ukbench100\ukbench00018.jpg
E:\Datasets\Ukbench100\ukbench00019.jpg
E:\Datasets\Ukbench100\ukbench00020.jpg
E:\Datasets\Ukbench100\ukbench00021.jpg
E:\Datasets\Ukbench100\ukbench00022.jpg
E:\Datasets\Ukbench100\ukbench00023.jpg
E:\Datasets\Ukbench100\ukbench00024.jpg
E:\Datasets\Ukbench100\ukbench00025.jpg
E:\Datasets\Ukbench100\ukbench00026.jpg
E:\Datasets\Ukbench100\ukbench00027.jpg
E:\Datasets\Ukbench100\ukbench00028.jpg
E:\Datasets\Ukbench100\ukbench00029.jpg
E:\Datasets\Ukbench100\ukbench00030.jpg
E:\Datasets\Ukbench100\ukbench00031.jpg
E:\Datasets\Ukbench100\ukbench00032.jpg
E:\Datasets\Ukbench100\ukbench00033.jpg
E:\Datasets\Ukbench100\ukbench00034.jpg
E:\Datasets\Ukbench100\ukbench00035.jpg
E:\Datasets\Ukbench100\ukbench00036.jpg
E:\Datasets\Ukbench100\ukbench00037.jpg
E:\Datasets\Ukbench100\ukbench00038.jpg
E:\Datasets\Ukbench100\ukbench00039.jpg
E:\Datasets\Ukbench100\ukbench00040.jpg
E:\Datasets\Ukbench100\ukbench00041.jpg
E:\Datasets\Ukbench100\ukbench00042.jpg
E:\Datasets\Ukbench100\ukbench00043.jpg
E:\Datasets\Ukbench100\ukbench00044.jpg
E:\Datasets\Ukbench100\ukbench00045.jpg
E:\Datasets\Ukbench100\ukbench00046.jpg
E:\Datasets\Ukbench100\ukbench00047.jpg
E:\Datasets\Ukbench100\ukbench00048.jpg
E:\Datasets\Ukbench100\ukbench00049.jpg
E:\Datasets\Ukbench100\ukbench00050.jpg
E:\Datasets\Ukbench100\ukbench00051.jpg
E:\Datasets\Ukbench100\ukbench00052.jpg
E:\Datasets\Ukbench100\ukbench00053.jpg
E:\Datasets\Ukbench100\ukbench00054.jpg
E:\Datasets\Ukbench100\ukbench00055.jpg
E:\Datasets\Ukbench100\ukbench00056.jpg
E:\Datasets\Ukbench100\ukbench00057.jpg
E:\Datasets\Ukbench100\ukbench00058.jpg
E:\Datasets\Ukbench100\ukbench00059.jpg
E:\Datasets\Ukbench100\ukbench00060.jpg
E:\Datasets\Ukbench100\ukbench00061.jpg
E:\Datasets\Ukbench100\ukbench00062.jpg
E:\Datasets\Ukbench100\ukbench00063.jpg
E:\Datasets\Ukbench100\ukbench00064.jpg
E:\Datasets\Ukbench100\ukbench00065.jpg
E:\Datasets\Ukbench100\ukbench00066.jpg
E:\Datasets\Ukbench100\ukbench00067.jpg
E:\Datasets\Ukbench100\ukbench00068.jpg
E:\Datasets\Ukbench100\ukbench00069.jpg
E:\Datasets\Ukbench100\ukbench00070.jpg
E:\Datasets\Ukbench100\ukbench00071.jpg
E:\Datasets\Ukbench100\ukbench00072.jpg
E:\Datasets\Ukbench100\ukbench00073.jpg
E:\Datasets\Ukbench100\ukbench00074.jpg
E:\Datasets\Ukbench100\ukbench00075.jpg
E:\Datasets\Ukbench100\ukbench00076.jpg
E:\Datasets\Ukbench100\ukbench00077.jpg
E:\Datasets\Ukbench100\ukbench00078.jpg
E:\Datasets\Ukbench100\ukbench00079.jpg
E:\Datasets\Ukbench100\ukbench00080.jpg
E:\Datasets\Ukbench100\ukbench00081.jpg
E:\Datasets\Ukbench100\ukbench00082.jpg
E:\Datasets\Ukbench100\ukbench00083.jpg
E:\Datasets\Ukbench100\ukbench00084.jpg
E:\Datasets\Ukbench100\ukbench00085.jpg
E:\Datasets\Ukbench100\ukbench00086.jpg
E:\Datasets\Ukbench100\ukbench00087.jpg
E:\Datasets\Ukbench100\ukbench00088.jpg
E:\Datasets\Ukbench100\ukbench00089.jpg
E:\Datasets\Ukbench100\ukbench00090.jpg
E:\Datasets\Ukbench100\ukbench00091.jpg
E:\Datasets\Ukbench100\ukbench00092.jpg
E:\Datasets\Ukbench100\ukbench00093.jpg
E:\Datasets\Ukbench100\ukbench00094.jpg
E:\Datasets\Ukbench100\ukbench00095.jpg
E:\Datasets\Ukbench100\ukbench00096.jpg
E:\Datasets\Ukbench100\ukbench00097.jpg
E:\Datasets\Ukbench100\ukbench00098.jpg
E:\Datasets\Ukbench100\ukbench00099.jpg