谈论Java递归列出文件目录结构

最近学Java,学到了File类,感觉很好用,于是就写个程序列出我搞OI时留下的代码文件的目录树。

import java.io.File;

public class TestFile {

        public static void main(String[] args) {
                File f = new File("D:/Temp/LYLtim_OI");
                System.out.println(f.getName());
                list(f, 0);
        }

        static void list(File f, int lv) {
                File[] childs = f.listFiles();
                for (int i = 0; i < childs.length; i++) {
                        for (int j = 0; j < lv; j++)
                                System.out.print(" ┃");
                        if (i == childs.length - 1)
                                System.out.println(" ┗" + childs[i].getName());
                        else
                                System.out.println(" ┣" + childs[i].getName());
                        if (childs[i].isDirectory())
                                list(childs[i], lv + 1);
                }
        }

}

┣Others

 ┃ ┣8no_bfs.pas
 ┃ ┣apples.pas
 ┃ ┣bag.pas
 ┃ ┣box.pas
 ┃ ┣budget.pas
 ┃ ┣dijkstra.pas
 ┃ ┣divisibility.pas
 ┃ ┣egg.pas
 ┃ ┣energy.pas
 ┃ ┣expand.pas
 ┃ ┣Fourcolor.pas
 ┃ ┣GF.pas
 ┃ ┣gfz.pas
 ┃ ┣hash.pas
 ┃ ┣horse.pas
 ┃ ┣jimuchengbao.pas
 ┃ ┣lcs.pas
 ┃ ┣magic.pas
 ┃ ┣medic.pas
 ┃ ┣message.pas
 ┃ ┣ministry.pas
 ┃ ┣money.pas
 ┃ ┣new_hanoi.pas
 ┃ ┣NOIp2001T4.pas
 ┃ ┣NOIp2002T4.pas
 ┃ ┣palin.pas
 ┃ ┣pane.pas
 ┃ ┣pane2.pas
 ┃ ┣party.pas
 ┃ ┣perform.pas
 ┃ ┣puzzle.pas
 ┃ ┣queue.pas
 ┃ ┣rebuild.pas
 ┃ ┣resume.pas
 ┃ ┣sequence.pas
 ┃ ┣short.pas
 ┃ ┣soldier.pas
 ┃ ┣spfa.pas
 ┃ ┣spy.pas
 ┃ ┣stone.pas
 ┃ ┣strs.pas
 ┃ ┣table.pas
 ┃ ┣tower.pas
 ┃ ┣tower2.pas
 ┃ ┣tree.pas
 ┃ ┣tree1.pas
 ┃ ┣tree2.pas
 ┃ ┣TYvj 1042.pas
 ┃ ┣TYvj 1043.pas
 ┃ ┣Ural 1167 Horses.pas
 ┃ ┣Vijos 1234.pas
 ┃ ┣WatchDVD.pas
 ┃ ┣whybert.pas
 ┃ ┣wird.pas
 ┃ ┣word.pas
 ┃ ┗words.pas
 ┣《实用算法基础教程》代码
 ┃ ┣!做题、问题记录.txt
 ┃ ┣a.in
 ┃ ┣BubbleSort.pas
 ┃ ┣gjdyh.pas
 ┃ ┣InsertSort.pas
 ┃ ┣MergeSort.pas
 ┃ ┣P100T6.pas
 ┃ ┣P100T7.pas
 ┃ ┣P110T1ex1.pas
 ┃ ┣P110T1ex2.pas
 ┃ ┣P110T1ex3.pas
 ┃ ┣P110T1ex4maze1.pas
 ┃ ┣P110T1ex4maze2.pas
 ┃ ┣P110T2.pas
 ┃ ┣P111T6.pas
 ┃ ┣P116ex2.pas
 ┃ ┣P12T1.pas
 ┃ ┣P12T2.pas
 ┃ ┣P12T3.pas
 ┃ ┣P145T1.pas
 ┃ ┣P145T2.pas
 ┃ ┣P146T10.pas
 ┃ ┣P146T10_2.pas
 ┃ ┣P146T3.pas
 ┃ ┣P146T4.pas
 ┃ ┣P146T5.pas
 ┃ ┣P146T7.pas
 ┃ ┣P146T8.pas
 ┃ ┣P146T9.pas
 ┃ ┣P155T1.pas
 ┃ ┣P155T2.pas
 ┃ ┣P156T3.pas
 ┃ ┣P179T1.pas
 ┃ ┣P179T2.pas
 ┃ ┣P179T3.pas
 ┃ ┣P193ex1.pas
 ┃ ┣P198ex2.pas
 ┃ ┣P202T1.pas
 ┃ ┣P20T2.pas
 ┃ ┣P20T3.pas
 ┃ ┣P20T4.pas
 ┃ ┣P20T5.pas
 ┃ ┣P211T1.pas
 ┃ ┣P211T2.pas
 ┃ ┣P22 R to 10.pas
 ┃ ┣P222T2.pas
 ┃ ┣P222T3.pas
 ┃ ┣P227ex1.pas
 ┃ ┣P23 10 real to R.pas
 ┃ ┣P233ex2.pas
 ┃ ┣P240ex3.pas
 ┃ ┣P264ex2.pas
 ┃ ┣P265ex3_kruskal.pas
 ┃ ┣P265ex3_prim.pas
 ┃ ┣P267ex4.pas
 ┃ ┣P31T1.pas
 ┃ ┣P31T2.pas
 ┃ ┣P31T4ex1.pas
 ┃ ┣P31T4ex2.pas
 ┃ ┣P31T4ex3.pas
 ┃ ┣P31T4ex4.pas
 ┃ ┣P48T1.pas
 ┃ ┣P48T2.pas
 ┃ ┣P55T1.pas
 ┃ ┣P55T2.pas
 ┃ ┣P56T3.pas
 ┃ ┣P56T4.pas
 ┃ ┣P56T5.pas
 ┃ ┣P56T6.pas
 ┃ ┣P62T1.pas
 ┃ ┣P62T2.pas
 ┃ ┣P62T3.pas
 ┃ ┣P62T4.pas
 ┃ ┣P62T5.pas
 ┃ ┣P62T6.pas
 ┃ ┣P72T1.pas
 ┃ ┣P73T2.pas
 ┃ ┣P73T3.pas
 ┃ ┣P73T4.pas
 ┃ ┣P73T5.pas
 ┃ ┣P73T6.pas
 ┃ ┣P79T1.pas
 ┃ ┣P80T3.exe
 ┃ ┣P80T3.pas
 ┃ ┣P80T4.pas
 ┃ ┣P80T5.pas
 ┃ ┣P80T6.pas
 ┃ ┣P80T7.pas
 ┃ ┣P88T1.pas
 ┃ ┣P88T2.pas
 ┃ ┣P88T3.pas
 ┃ ┣P88T4.pas
 ┃ ┣P88T5.pas
 ┃ ┣P98T1.pas
 ┃ ┣P98T2.pas
 ┃ ┣P98T3.pas
 ┃ ┣P99T4.pas
 ┃ ┗QSort.pas
 ┣《实用算法基础教程》 虚拟主机配套练习
 ┃ ┣1.基础
 ┃ ┃ ┣apple.pas
 ┃ ┃ ┣ball.pas
 ┃ ┃ ┣password.pas
 ┃ ┃ ┣save.pas
 ┃ ┃ ┣scholar.pas
 ┃ ┃ ┣series.pas
 ┃ ┃ ┣sum.pas
 ┃ ┃ ┣switch.pas
 ┃ ┃ ┣train.pas
 ┃ ┃ ┣tree.pas
 ┃ ┃ ┗unhappy.pas
 ┃ ┣11.分治
 ┃ ┃ ┣car.pas
 ┃ ┃ ┗equ.pas
 ┃ ┣13.广搜
 ┃ ┃ ┣cell.pas
 ┃ ┃ ┣coin.pas
 ┃ ┃ ┣save.pas
 ┃ ┃ ┗turn.pas
 ┃ ┣14.动归
 ┃ ┃ ┣boxes.pas
 ┃ ┃ ┣chorus.pas
 ┃ ┃ ┣flower.pas
 ┃ ┃ ┣knight.pas
 ┃ ┃ ┣mine.pas
 ┃ ┃ ┣missile.pas
 ┃ ┃ ┣ship.pas
 ┃ ┃ ┣short.pas
 ┃ ┃ ┣tower.pas
 ┃ ┃ ┗weight.pas
 ┃ ┣2.枚举
 ┃ ┃ ┣apple.pas
 ┃ ┃ ┣num.pas
 ┃ ┃ ┣number.pas
 ┃ ┃ ┣number1.pas
 ┃ ┃ ┣pnumber.pas
 ┃ ┃ ┗weight.pas
 ┃ ┣3.进制
 ┃ ┃ ┣change.pas
 ┃ ┃ ┣mchange.pas
 ┃ ┃ ┣nchange.pas
 ┃ ┃ ┣num.pas
 ┃ ┃ ┗sequence.pas
 ┃ ┣4.高精度
 ┃ ┃ ┣multiply.pas
 ┃ ┃ ┣sum.pas
 ┃ ┃ ┗yubikili.pas
 ┃ ┣5.排序
 ┃ ┃ ┣carry.pas
 ┃ ┃ ┣count.pas
 ┃ ┃ ┣fruit.pas
 ┃ ┃ ┣knumber.pas
 ┃ ┃ ┣masses.pas
 ┃ ┃ ┣random.pas
 ┃ ┃ ┣scholar.pas
 ┃ ┃ ┣secret.pas
 ┃ ┃ ┗subset.pas
 ┃ ┣7.递推
 ┃ ┃ ┣domino.pas
 ┃ ┃ ┣knight.pas
 ┃ ┃ ┣rabbit.pas
 ┃ ┃ ┣stairs.pas
 ┃ ┃ ┣surface.pas
 ┃ ┃ ┗tower.pas
 ┃ ┣9.回溯
 ┃ ┃ ┣compages.pas
 ┃ ┃ ┣decompose.pas
 ┃ ┃ ┣form.pas
 ┃ ┃ ┗queen.pas

 ┃ ┗做题记录.txt

/**
 * 列出指定目录的全部内容
 * */
import java.io.*;
class hello{
    public static void main(String[] args) {
        String fileName="D:"+File.separator;
        File f=new File(fileName);
        print(f);
    }
    public static void print(File f){
        if(f!=null){
            if(f.isDirectory()){
                File[] fileArray=f.listFiles();
                if(fileArray!=null){
                    for (int i = 0; i < fileArray.length; i++) {
                        //递归调用
                        print(fileArray[i]);
                    }
                }
            }
            else{
                System.out.println(f);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值