繁体   English   中英

Java中的unix ls命令

[英]unix ls command in java

嗨,我正在尝试在Java中以unix“ ls”命令样式列出文件夹。 我有一个像这样的文件夹树:

在此处输入图片说明

我的文件结构如下:

private class Node{

    public List<Node> children= new ArrayList<Node>();
    public String value;
    public Node(String value){
        this.value = value;
    }

    public void addSubDirectory(Node child){
        children.add(child);
    }

}

问题:当命令中的“ *”后没有任何内容时,我不能递归地超过当前文件夹的孩子

例如。 命令

ls A / B / * / * / E /和ls A / * / * / M / * / E /

但是命令“ ls A / *”仅显示

通过

遍历代码

public void showCommand(Node root, String command){
        String argument = command.substring(3);
        System.out.println("command is ls "+argument);
        show(root,argument);   
    }
    private void show(Node root, String cmd){

        int N = cmd.length();
        if(N==0){
            // case when end of command is reached
            for(Node child:root.children){
                System.out.println(child.value);
            }
        }else{
            char c = cmd.charAt(0);
            // case when folder name is present in command
            if((c!='*') && (c!='/')){
                for(Node child:root.children){
                    if(child.value.equals(String.valueOf(c))){
                        show(child,cmd.substring(1));
                    }
                }
                return;
            }else if(c=='*'){  
            // case when * is present in command
            // i think problem lies here
                if(N==1){
                   System.out.println(root.value);
                   preOrderTraverse(root);
                }else{
                  for(Node child:root.children){
                      show(child,cmd.substring(1));
                  }
                }
                return;
            }else if(c=='/'){
            // case when '/' is present in commmand
                show(root,cmd.substring(1));
                return;
            }

        }
    }

当命令中的最后一个字符为*时,您将使用空cmd调用showCommand方法,并且当该方法接收到空cmd字符串时,递归将停止。

当当前字符c为*时,应检查它是cmd的最后一个字符,如果是,则应发送当前cmd而不是cmd.substring(1) 这样,它应该递归地遍历层次结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM