weixin_42362593 2010-04-09 13:52
浏览 225
已采纳

按照特定格式,列出目录下面所有的子目录和文件

做一个程序,输入一个参数:开始目录,把此目录下的所有目录和文件结构导入到export.txt文件,结构如下
c:\目录1
****|_目录11
****|_目录12
****|*** |_目录121
****|_目录33
****|**|_目录122
****|**|_目录132
****|***** |_目录532
****|******** |_file.exe
****|_file.doc
****|_file2.txt

我知道用递归可以列出来,但是要按照上面的这种格式,怎么都想不出来,请教各位!注意:上面格式的特点是,同一个目录下的目录和文件要连在一起的。这个不会,望解答!谢谢!
(还有一点:格式里的星号原本是空格来的,但是发表时空格被去掉了,所以我用同样长度的星号代替,请高手改回空格,多谢啦!)

  • 写回答

4条回答 默认 最新

  • iteye_13500 2010-04-09 16:44
    关注

    [code="java"]import java.io.File;
    import java.util.*;

    public class TestTree {

    public static class TreeNode {
        private File file;
        private int level;
        private boolean isEnd;
    
        public File getFile() {
            return file;
        }
    
        public void setFile(File file) {
            this.file = file;
        }
    
        public int getLevel() {
            return level;
        }
    
        public void setLevel(int level) {
            this.level = level;
        }
    
        public boolean isEnd() {
            return isEnd;
        }
    
        public void setEnd(boolean isEnd) {
            this.isEnd = isEnd;
        }
    
        public TreeNode(File fileNode, int level, boolean isEnd) {
            this.file = fileNode;
            this.level = level;
            this.isEnd = isEnd;
        }
    
        @Override
        public String toString() {
            return "[" + file.getPath() + "," + level + "," + isEnd + "]";
        }
    }
    
    public static void main(String[] args) {
        File f = new File("D:/test");
        TreeNode root = new TreeNode(f, 0, true);
    
        int cursor = 0;
        List<TreeNode> list = new ArrayList<TreeNode>();
        list.add(root);
    
        do {
            tree(cursor, list);
            cursor++;
        } while (cursor < list.size());
    
        boolean[] haveLine = new boolean[20];
        int level;
        String line;
        for (int i = 0; i < list.size(); i++) {
            line = "";
            level = list.get(i).getLevel();
            for (int j = 0; j < level; j++) {
                if (haveLine[j]) {
                    line = line + "|***";
                } else {
                    line = line + "****";
                }
            }
    
            haveLine[level] = !list.get(i).isEnd();
    
            if (level != 0) {
                line = line + "|_";
            }
    
            line = line + list.get(i).getFile().getPath();
            System.out.println(line);
        }
    
    }
    
    private static void tree(int currentCursor, List<TreeNode> list) {
        File file = list.get(currentCursor).getFile();
    
        if (!file.isDirectory()) {
            return;
        }
        int newLevel = list.get(currentCursor).getLevel() + 1;
    
        List<TreeNode> newList = new ArrayList<TreeNode>();
        File[] childs = file.listFiles();
    
        for (int i = 0; i < childs.length; i++) {
            boolean isEnd = false;
            if (i == childs.length - 1) {
                isEnd = true;
            }
            TreeNode node = new TreeNode(childs[i], newLevel, isEnd);
            newList.add(node);
        }
        list.addAll(currentCursor + 1, newList);
    }
    

    }[/code]

    测试过可用:
    D:\test
    ****|_D:\test\a
    ****|***|_D:\test\a\e
    ****|***|_D:\test\a\f.txt
    ****|_D:\test\b
    ****|_D:\test\c
    ****|_D:\test\a.txt

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程