很簡單,寫出來記錄一下
public class F {
/**
*
* @param file 文件
* @param c 用於記錄製表符
*/
public static void getFileName(File file, String c){
/**
* 如果是文件夾,打印名稱(帶上製表符)
*/
if(file.isDirectory()){
System.out.println(c + file.getName());
}
/**
* 獲取所有子文件
*/
File[] files = file.listFiles();
for(File f : files){
/**
* 首先加一個製表符
*/
String temp = c + "\t";
if(f.isDirectory()){
/**
* 如果是文件夾,則進行遞歸
*/
getFileName(f, temp);
} else {
/**
* 如果不是文件夾,則直接打印
*/
System.out.println(temp + f.getName());
}
}
}
public static void main(String[] args) throws IOException {
getFileName(new File("F:\\upan"), "");
}
}