Java -- 获取指定目录下的所有文件
1. 通过console 输入要搜索文件的目录
2. Invoke getFileName 方法获取该目录下的所有文件
package com.basic.io;
import java.io.File;
import java.util.Scanner;
public class GetFileFromTargetPath {
private static Scanner scanner;
//Get Files from the target path
public static void getFileName(String path){
File file = new File(path);
if (file.isDirectory()) {
File[] dirFiles = file.listFiles();
for (File f : dirFiles) {
if (f.isDirectory()) {
//recursively call the getFileName method is path still a directory
getFileName(f.getAbsolutePath());
} else {
//print the file name of the target path
System.out.println(f.getAbsolutePath());
}
}
} else {
//print the file name of the target path
System.out.println(file.getAbsolutePath());
}
}
public static void main(String[] args) {
scanner = new Scanner(System.in);
//input the files path from console
System.out.print("Please input the files path :");
String filePath = scanner.next();
//invoke the getFileName method
getFileName(filePath);
}
}