2014-07-05 6 views
0

안녕하세요, 나는 유닉스에서 "ls"명령어 스타일을 java에 나열하려고합니다.유닉스 ls 명령어 in java

enter image description here

내 파일 구조는 다음과 같다 :

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

     } 
    } 
+0

당신도 알다시피, File 클래스는 폴더의 모든 파일과 디렉토리를 검색 할 수있는 많은 메소드를 제공합니다 ... –

+0

안녕하세요, 파일을 모델로 트리를 만들고 파일 탐색을 사용하여 모든 파일을 방문하려고합니다. – sreeprasad

답변

1

명령의 마지막 문자가 *이면 빈 cmd를 사용하여 showCommand 메서드를 다시 호출하고 메서드에서 빈 cmd 문자열을 받으면 재귀가 중지됩니다.

현재 문자 c가 * 인 경우, 당신이 다음의 경우에는 cmd.substring(1) 대신 현재 cmd을 보내야합니다, 그것은 cmd의 마지막 샤아 확인해야합니다. 그런 식으로 재귀 적으로 계층 구조를 거쳐야합니다.

+0

아이디어는 작은 변화와 함께 일했습니다. '*'가 명령의 마지막 문자이고 다른 방법으로 show 메서드를 호출하면 간단한 preOrder 순회를 작성했습니다. 수정 된 코드를 편집하고 업데이트했습니다. – sreeprasad