2012-03-12 5 views
-5

Centos에 /etc/yum.repos.d 디렉토리의 내용을 표시하려고합니다. 보통이 디렉토리에는 Centos가 RPM 패키지를 다운로드하는 저장소에 대한 정보가 저장됩니다. 디렉토리의 내용을 Java로 표시하는 올바른 방법은 무엇입니까?Java - 디렉토리의 내용을 나열하는 방법?

보다도 여기

+3

새로운 비슷한 사람을 만드는 대신 기존 질문을 수정하시기 바랍니다. –

+2

http://docs.oracle.com/javase/tutorial/essential/io/fileio.html –

+0

4 단어로 된 Google 검색에서 쉽게 해결할 수없는 질문을보고 싶습니다. 계속 봐야 할 것 같네요. –

답변

3
import java.io.File; 
import java.util.Arrays; 

public class Dir { 
    static int indentLevel = -1; 

    static void listPath(File path) { 
    File files[]; 
    indentLevel++; 

    files = path.listFiles(); 

    Arrays.sort(files); 
    for (int i = 0, n = files.length; i < n; i++) { 
     for (int indent = 0; indent < indentLevel; indent++) { 
     System.out.print(" "); 
     } 
     System.out.println(files[i].toString()); 
     if (files[i].isDirectory()) { 

     listPath(files[i]); 
    } 
    } 
indentLevel--; 
} 

public static void main(String args[]) { 
    listPath(new File("/etc/yum.repos.d")); 
} 
} 
3

소원 예입니다

File directory = new File("/etc/yum.repos.d"); 
File[] files = directory.listFiles(); 
3
File dir = new File("some_directory"); 
String[] filesAndSubdirs = dir.list(); 
관련 문제