1 import java.io.File; 2 3 public class Demo1 { 4 public static void main(String[] args) { 5 File file=new File("c:");//建立c盤目錄這個對象 6 getAllJavaFiles(file); 7 } 8 public static void getAllJavaFiles(File file){ 9 File []fileArr=file.listFiles(); 10 for(int i=0;i<fileArr.length;i++){ 11 if(fileArr[i].isFile()){ 12 if(fileArr[i].getName().endsWith(".java")){ //判斷文件名這個字符串的後綴是否爲「.java」 13 //打印文件的相對路徑 14 System.out.println(fileArr[i].getAbsolutePath()); 15 } 16 } else { 17 getAllJavaFiles(fileArr[i]);//遞歸 18 } 19 } 20 } 21 }
結果:java
1 c:\java練習代碼\Exception\ExceptionDemo\ExceptionDemo1.java 2 c:\java練習代碼\Exception\ExceptionDemo\ExceptionDemo2.java 3 c:\java練習代碼\Exception\ExceptionDemo\ExceptionDemo3.java 4 c:\java練習代碼\Exception\ExceptionDemo\MyException.java 5 c:\java練習代碼\Exception\ExceptionDemo\StudentDemo.java 6 c:\java練習代碼\Exception\ExceptionDemo\teacher.java 7 c:\java練習代碼\File\src\Demo.java 8 c:\java練習代碼\File\src\Demo1.java 9 c:\java練習代碼\File\src\FileDemo1.java 10 c:\java練習代碼\File\src\FileDemo2.java 11 c:\java練習代碼\File\src\FileDemo3.java 12 c:\java練習代碼\File\src\FileDemo4.java 13 c:\java練習代碼\File\src\FileDemo5.java 14 c:\java練習代碼\File\src\FileDemo6.java 15 c:\java練習代碼\File\src\FileDemo7.java 16 c:\java練習代碼\Set\CollectionDemo\ListDemo\ListDemo1.java 17 c:\java練習代碼\Set\CollectionDemo\ListDemo\ListDemo2.java 18 c:\java練習代碼\Set\CollectionDemo\ListDemo\Student.java 19 c:\java練習代碼\Set\CollectionDemo\ListDemo\StudentDemo.java 20 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\CollectionDemo1.java 21 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\CollectionDemo2.java 22 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\CollectionDemo3.java 23 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\CollectionDemo4.java 24 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\ObjectArray1.java 25 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\StudengDemo.java 26 c:\java練習代碼\Set\CollectionDemo\ObjectArrayDemo\Student.java 27 c:\java練習代碼\string\ArrayDemo\Basic.java 28 c:\java練習代碼\string\ArrayDemo\CharacterDemo.java 29 c:\java練習代碼\string\ArrayDemo\IntegerDemo.java 30 c:\java練習代碼\string\BigDecimalDemo\BigDecimal1.java 31 c:\java練習代碼\string\BigIntegerDemo\BigInteger1.java 32 c:\java練習代碼\string\DateDemo\basic.java 33 c:\java練習代碼\string\DateDemo\DateFormatDemo.java 34 c:\java練習代碼\string\DateDemo\DateUtil.java 35 c:\java練習代碼\string\DateDemo\DateUtilDemo.java 36 c:\java練習代碼\string\DateDemo\DayCount.java 37 c:\java練習代碼\string\MathDemo\commom.java 38 c:\java練習代碼\string\MathDemo\Random1.java 39 c:\java練習代碼\string\ObjectDemo\Student.java 40 c:\java練習代碼\string\ObjectDemo\StudentTest.java 41 c:\java練習代碼\string\RegexDemo\Cut.java 42 c:\java練習代碼\string\RegexDemo\Judge.java 43 c:\java練習代碼\string\RegexDemo\Replace.java 44 c:\java練習代碼\string\RegexDemo\Sort.java 45 c:\java練習代碼\string\ScannerDemo\Scanner1.java 46 c:\java練習代碼\string\ScannerDemo\Scanner2.java 47 c:\java練習代碼\string\StringBufferDemo\Add.java 48 c:\java練習代碼\string\StringBufferDemo\Construction.java 49 c:\java練習代碼\string\StringBufferDemo\Cut.java 50 c:\java練習代碼\string\StringBufferDemo\Delete.java 51 c:\java練習代碼\string\StringBufferDemo\Replace.java 52 c:\java練習代碼\string\StringBufferDemo\Reverse.java 53 c:\java練習代碼\string\StringBufferDemo\SplicingCase.java 54 c:\java練習代碼\string\StringDemo\anli\chaZhaoCiShu.java 55 c:\java練習代碼\string\StringDemo\anli\chazhaozifushu.java 56 c:\java練習代碼\string\StringDemo\anli\daoxu.java 57 c:\java練習代碼\string\StringDemo\anli\dichotomy.java 58 c:\java練習代碼\string\StringDemo\anli\panduanqqdenglu.java 59 c:\java練習代碼\string\StringDemo\anli\phonejiaoyan.java 60 c:\java練習代碼\string\StringDemo\anli\pinjie.java 61 c:\java練習代碼\string\StringDemo\anli\qqjiaoyan.java 62 c:\java練習代碼\string\StringDemo\anli\redex1.java 63 c:\java練習代碼\string\StringDemo\huoqu\fangfa.java 64 c:\java練習代碼\string\StringDemo\huoqu\StringDemo3.java 65 c:\java練習代碼\string\StringDemo\huoqu\tongjigeshu.java 66 c:\java練習代碼\string\StringDemo\otherfunction\StringDemo1.java 67 c:\java練習代碼\string\StringDemo\panduan\StringDemo2.java 68 c:\java練習代碼\string\StringDemo\zhuanhuan\StringDemo4.java 69 c:\java練習代碼\string\SystemDemo\arraycopy.java 70 c:\java練習代碼\案例區\src\AnimalTest.java 71 c:\java練習代碼\案例區\src\CaiShu.java 72 c:\java練習代碼\案例區\src\Fibonacci.java 73 c:\java練習代碼\案例區\src\JiaMi.java 74 c:\java練習代碼\案例區\src\jiekouceshi.java 75 c:\java練習代碼\案例區\src\KongZiTest.java 76 c:\java練習代碼\案例區\src\NorthSouth.java 77 c:\java練習代碼\案例區\src\StudentDate.java 78 c:\java練習代碼\案例區\src\SwitchDemo.java 79 80 Process finished with exit code 0