要求打印效果如下所示:
C:\q3r.vsdx
C:\qwer.txt
-C:\sss
C:\sss\adf.txt
C:\sss\etqs.txt
-C:\ttt
-C:\ttt\aaa
C:\ttt\aaa\abc.txt
C:\ttt\nrgf.txt
C:\ttt\qwerq.docx
代码中使用了递归
代码如下:
import java.io.File; /** * Created by YZT on 2018/1/13 0013. */ public class getAllPathAndFileName { public static void main(String[] args) throws Exception { File f = new File("C:\\linshi"); getAllPathAndFileName mgetAllFileNameAndPath = new getAllPathAndFileName(); mgetAllFileNameAndPath.getDirectorys(f, 0); } public void getDirectorys(File f, int level) throws Exception { String space = getSpace(++level); File[] l = f.listFiles(); if (l.length == 0)