반응형

디렉토리 및 하위 디렉토리에 있는 파일목록 추출하기

 

 

재귀함수를 이용하여 지정한 디렉토리 및 하위디렉토리의 파일까지 추출하는 프로그램입니다.
디렉토리에서 파읽을 읽는 로직은 동일하며, 객체유형이 디렉토리일 경우, 재귀함수를 호출하여 파일을 검색합니다.

* SOURCE

    public static void main(String[] args) { 
         
        String strDirPath = "C:\\workspace\\Test\\"; 
         
        ListFile( strDirPath ); 
    } 
     
    // 재귀함수 
    private static void ListFile( String strDirPath ) { 
         
        File path = new File( strDirPath ); 
        File[] fList = path.listFiles(); 
         
        for( int i = 0; i < fList.length; i++ ) { 
             
            if( fList[i].isFile() ) { 
                System.out.println( fList[i].getPath() );  // 파일의 FullPath 출력 
            } 
            else if( fList[i].isDirectory() ) { 
                ListFile( fList[i].getPath() );  // 재귀함수 호출 
            } 
        } 
    } 


* RESULT

    C:\workspace\Test\A.txt 
    C:\workspace\Test\B.txt 
    C:\workspace\Test\C.txt 
    C:\workspace\Test\Sub1\Sub1_A.txt 
    C:\workspace\Test\Sub1\Sub1_B.txt 
    C:\workspace\Test\Sub2\Sub2_B.txt 
    C:\workspace\Test\Sub2\Sub2_C.txt 


※ 참고

 

    - 링크 ☞ 디렉토리에 있는 파일목록 추출하기
    - 링크 ☞ 재귀함수( Recycle-Function )

끝.

반응형