unixls命令在java中的应用

unixls命令在java中的应用,java,tree,Java,Tree,您好,我正在尝试用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

您好,我正在尝试用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);
    }

}

当命令中的最后一个字符是*时,使用空cmd回调
showCommand
方法,当该方法收到空cmd字符串时,递归停止


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

你知道,File类提供了很多方法,可以检索文件夹中的所有文件和目录…Hi Mark,只是尝试将文件建模为一棵树,并使用文件遍历访问所有文件。你的想法做了一个小改动。当“*”是命令的最后一个字符时,我编写了简单的前序遍历,并以其他方式调用了show方法。我已编辑并更新了更正的代码
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;
            }

        }
    }