import java.io.*; 02 03 public class TestIO{ 04 public static void main(String[] args){ 05 File f = new File( "d:/javaFile" ); 06 tree(f); 07 } 08 //顯示目錄的方法 09 public static void tree(File f){ 10 //判斷傳入對象是否為一個檔案夾對象 11 if (!f.isDirectory()){ 12 System.out.println( "你輸入的不是一個檔案夾,請檢查路徑是否有誤!!" ); 13 } 14 else { 15 File[] t = f.listFiles(); 16 for ( int i= ;i<t.length;i++){ 17 //判斷檔案清單中的對象是否為檔案夾對象,如果是則執行tree遞歸,直到把此檔案夾中所有檔案輸出為止 18 if (t[i].isDirectory()){ 19 System.out.println(t[i].getName()+ "\tttdir" ); 20 tree(t[i]); 21 } 22 else { 23 System.out.println(t[i].getName()+ "tFile" ); 24 } 25 } 26 } 27 28 } 29 }