递归遍历d盘所有文件

package Io流;


import org.junit.Test;

import java.io.File;

public class Demo02 {
    public static void showFile(String pathName){
        File file = new File(pathName);
        if (file.isDirectory()){
            File[] files = file.listFiles();
            if (files!=null){
                for (File f:files) {
                    if (f.isDirectory()){
                        String path = f.getPath();
                        showFile(path);
                    }else {
                        String name = file.getName();
                        String path = file.getAbsolutePath();
                        System.out.println(name+"文件的路径是"+path);
                    }
                }
            }

        }else {
            String name = file.getName();
            String path = file.getAbsolutePath();
            System.out.println(name+"文件的路径是"+path);
        }
    }
    /**
     * 方法递归
     * 1.判断文件是否是文件夹  isDirect();
     * 2.listFiles()---->file[]
     */
    public static void main(String[] args) {
        String pathName = "E:\\";
        showFile(pathName);
    }
}
posted @ 2021-01-24 11:25  dgxacc  阅读(170)  评论(0)    收藏  举报