2012-05-17 4 views
0

임의의 중첩 폴더 및 파일 세트에서 특정 하위 폴더의 이름이 "x"인 모든 폴더 경로를 가져와야합니다.특정 폴더 경로 얻기

예를 들어, 구조를 다음에, 나는 프로그램이 다시에만 a\c\e\x 반환해야합니다

enter image description here

중첩의 모든 수준 및 폴더 구조가있을 수 있음을 참고하시기 바랍니다.

업데이트 : 나는 원하는 것을 얻기 위해 기본 프로그램을 작성할 수있었습니다. 그러나 이전 다이어그램에 'dir d'아래에 'dir x'라는 디렉토리가있을 때 실패합니다. 따라서 프로그램은 \ c \ e \ x와 d \ x를 반환합니다. 그 수정을 도울 수 있습니까?

공용 클래스 좀비 {

String folderName= new String(); 
ArrayList folderList= new ArrayList(); 

public static void main(String[] args) { 
    String path; 
    try { 
     path = new java.io.File(".").getCanonicalPath(); 
     Zombie zombie= new Zombie(); 
     getFilePaths(new File(path).listFiles(), zombie); 


     Iterator itr = zombie.folderList.iterator(); 
     while (itr.hasNext()) 
      System.out.println(itr.next()); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public static void getFilePaths(File[] fileList, Zombie zombie) { 
    for (int i=0; i<fileList.length; i++) { 
     File file= fileList[i]; 
     if (file.isDirectory()) { 
      if (containsDesiredFolder(file)) { 
       zombie.folderList.add(zombie.folderName+ "\\" + file.getName()); 
       zombie.folderName=""; 
      } else { 
       if (containsFoldersNotFiles(file)) { 
        zombie.folderName= zombie.folderName + "\\" + file.getName(); 
        getFilePaths(file.listFiles(), zombie); 
       } 
      } 
     } 
    } 
} 

public static boolean containsFoldersNotFiles(File file) { 
    boolean flag = false; 
    if (file.isFile()) return false; 

    File[] dummyList = file.listFiles(); 

    //no file or folder present 
    if (dummyList == null) 
     return flag; 

    for (int i = 0; i < dummyList.length; i++) { 
     File dummyFile = dummyList[i]; 
     if (dummyFile.isDirectory()) { 
      return true; 
     } 
    } 
    return flag; 
} 

public static boolean containsDesiredFolder(File file) { 
    boolean flag = false; 
    if (file.isFile()) return false; 
    File[] dummyList = file.listFiles(); 

    if (dummyList == null) 
     return flag; 

    for (int i = 0; i < dummyList.length; i++) { 
     File dummyFile = dummyList[i]; 
     if (dummyFile.isDirectory() && dummyFile.getName().equals("dirx")) { 
      flag = true; 
     } 
    } 
    return flag; 
} 

}

+0

주로 파일과 폴더를 보관하는 데 사용되는 데이터 구조에 따라 달라지며 여기에 사용 된 데이터 구조는 무엇입니까? –

답변

0
  1. 걸릴 씨 폴더를 입력 한 문자열의 folder_list 변수에 이름을 추가합니다.
  2. 시드 폴더가 x이고 자식 노드가 folder_list를 전역 folder_array에 추가하면.
  3. 시드 폴더의 모든 폴더를 트래버스합니다. folder_list를 전달하고 모든 하위 폴더를 시드 폴더로 전달하는 동안 1에서 반복합니다.

추신 : 이것은 거의 할 것입니다.