具体代码如下:
import java.io.File; public class FileDemo { public static void main(String[] args) { File file = new File("pathName"); getNames(file); } /** * 比较简单,感觉不需要什么注释吧 */ public static void getNames(File path) { File[] files = path.listFiles(); for (int i = 0; i < files.length; i++) { System.out.println(files[i].getAbsolutePath()); if (files[i].isDirectory()) { getNames(files[i]); } } } }输出结果:
/Users/testUser/Downloads/笔记/.DS_Store /Users/testUser/Downloads/笔记/笔记 /Users/testUser/Downloads/笔记/笔记/Spring5框架课堂笔记.pdf /Users/testUser/Downloads/笔记/笔记/分析图 /Users/testUser/Downloads/笔记/笔记/分析图/图1.png /Users/testUser/Downloads/笔记/笔记/分析图/图3.png /Users/testUser/Downloads/笔记/笔记/分析图/图2.png /Users/testUser/Downloads/笔记/笔记/分析图/图6.png /Users/testUser/Downloads/笔记/笔记/分析图/.DS_Store /Users/testUser/Downloads/笔记/笔记/分析图/图7.png /Users/testUser/Downloads/笔记/笔记/分析图/图5.png /Users/testUser/Downloads/笔记/笔记/分析图/图4.png /Users/testUser/Downloads/笔记/笔记/分析图/Spring5模块.bmp /Users/testUser/Downloads/笔记/笔记/分析图/图8.png /Users/testUser/Downloads/笔记/笔记/分析图/事务 /Users/testUser/Downloads/笔记/笔记/分析图/事务/事务传播行为.bmp /Users/testUser/Downloads/笔记/笔记/分析图/事务/事务隔离级别.bmp /Users/testUser/Downloads/笔记/笔记/.DS_Store Process finished with exit code 0