안녕하세요, 나는 유닉스에서 "ls"명령어 스타일을 java에 나열하려고합니다.유닉스 ls 명령어 in java
내 파일 구조는 다음과 같다 :
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; } } }
당신도 알다시피, File 클래스는 폴더의 모든 파일과 디렉토리를 검색 할 수있는 많은 메소드를 제공합니다 ... –
안녕하세요, 파일을 모델로 트리를 만들고 파일 탐색을 사용하여 모든 파일을 방문하려고합니다. – sreeprasad