使用 java 打印文件树状目录

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2023-04-19 22:41
  • 2628人已阅读
简介 先上文件树状目录图:根据《Thinkinjava》io章的前两节,简单写出来的代码,还有不完善的地方,需要修改。代码如下:import java.util.regex.*;import java.io.*;import java.util.*;public class Directory {    

🔔🔔🔔好消息!好消息!🔔🔔🔔

有需要的朋友👉:联系凯哥 微信号 kaigejava2022

先上文件树状目录图:

4f310cea1cc0b0be11776051c3719ca4.png

根据 《Think in java》 io 章的前两节,简单写出来的代码, 还有不完善的地方,需要修改。

代码如下:

import java.util.regex.*;
import java.io.*;
import java.util.*;

public class Directory {
    private File currentDirectoryFile;
    private LinkedList<File> beforeDirectories = new LinkedList<File>();

    public Directory(String start) {
        currentDirectoryFile = new File(start);
        beforeDirectories.add(currentDirectoryFile);
    }

    class FileComparator implements Comparator<File> {
        public int compare(File one, File two) {
            String oneString = one.getName();
            String twoString = two.getName();
            return oneString.compareTo(twoString);
        }
    }

    private class TreeInfo {
        private List<File> files = new ArrayList<File>();
        private List<File> dirs = new ArrayList<File>();
    }

    private TreeInfo addAll() {
        // 将 currentDirectoryFile 此目录中的所有文件与文件夹提取保存,并分别按照从小到大的顺序排列。
        TreeInfo tInfo = new TreeInfo();
        File[] allFiles = currentDirectoryFile.listFiles();
        Arrays.sort(allFiles, new FileComparator());
        for(File elem: allFiles) {
            if(elem.isFile())
                tInfo.files.add(elem);
            else
                tInfo.dirs.add(elem);
        }
        return tInfo;
    }

    private String filesPrint(File f) {
        String s = "";
        for(int i = 0; i < beforeDirectories.size(); i++) {
            int spaceLength;
            if (i == 0) {
                spaceLength = beforeDirectories.get(i).getAbsolutePath().length();
            } else {
                spaceLength = beforeDirectories.get(i).getName().length() + 2;
            }
            s = String.format(s + "%" + spaceLength + "s|", "");
        }
        s += "_ %s";
        return String.format(s, f.getName());
    }

    private void printTreeInfoInSingleDirectory() {
        TreeInfo tInfo = addAll();
        for(File f: tInfo.files) {
            System.out.println(filesPrint(f));
        }
        for(File d: tInfo.dirs) {
            System.out.println(filesPrint(d));
            beforeDirectories.addLast(d);
            currentDirectoryFile = d;
            printTreeInfoInSingleDirectory();
            beforeDirectories.removeLast();
        }
    }

    public void walk() {
        System.out.println(currentDirectoryFile.getAbsolutePath());
        printTreeInfoInSingleDirectory();
    }

    public static void main(String[] args) {
        Directory t = new Directory("G:\\react\\node_modules\\redux");
        t.walk();
    }
}


输出的结果如下:

G:\react\node_modules\redux
                           |_ CHANGELOG.md
                           |_ LICENSE.md
                           |_ README.md
                           |_ index.d.ts
                           |_ package.json
                           |_ dist
                           |      |_ redux.js
                           |      |_ redux.min.js
                           |_ es
                           |    |_ redux.js
                           |_ lib
                           |     |_ redux.js
                           |_ node_modules
                           |              |_ .bin
                           |              |      |_ loose-envify
                           |              |      |_ loose-envify.cmd
                           |_ src
                           |     |_ applyMiddleware.js
                           |     |_ bindActionCreators.js
                           |     |_ combineReducers.js
                           |     |_ compose.js
                           |     |_ createStore.js
                           |     |_ index.js
                           |     |_ utils
                           |     |       |_ actionTypes.js
                           |     |       |_ isPlainObject.js
                           |     |       |_ warning.js

Process finished with exit code 0

TopTop