简体   繁体   English

Java中的unix ls命令

[英]unix ls command in java

Hi I am trying to list folders in unix "ls" command style in java. 嗨,我正在尝试在Java中以unix“ ls”命令样式列出文件夹。 I have a folder tree like this : 我有一个像这样的文件夹树:

在此处输入图片说明

My file structure is as follows : 我的文件结构如下:

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);
    }

}

Problem : I am not able to recurse more than the children of current folder when there is nothing after the ' * ' in a command 问题:当命令中的“ *”后没有任何内容时,我不能递归地超过当前文件夹的孩子

For eg. 例如。 command 命令

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

But command " ls A/ * " only shows 但是命令“ ls A / *”仅显示

BY 通过

Code to traverse 遍历代码

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;
            }

        }
    }

When the last char in the command is a *, you call back the showCommand method with an empty cmd and when the method receives an empty cmd string the recursion stops. 当命令中的最后一个字符为*时,您将使用空cmd调用showCommand方法,并且当该方法接收到空cmd字符串时,递归将停止。

When the current char c is a *, you should check it's the last char in cmd , if it is then you should send the current cmd instead of cmd.substring(1) . 当当前字符c为*时,应检查它是cmd的最后一个字符,如果是,则应发送当前cmd而不是cmd.substring(1) That way it should go through the hierarchy recursively. 这样,它应该递归地遍历层次结构。

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

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