2016-08-23 1 views
0

안녕하세요. 제 영어는 유감스럽게 생각하지만 도움이 필요합니다. 그러나 이것은 나에게 쉽지는 않을 것입니다. 내가하려고하는 일은 디스크 파티션을 나열하고 그 안에있는 디렉토리와 파일의 이름을 모두 나열하는 것입니다. 내가 하드 디스크 divitions하고있는 파일의 첫 번째 행을 나열 받기로디스크 파티션에서 파일 이름 인쇄하기

File[] root= File.listRoots();   

System.out.println("Se encontraron " + root.length + " Particiones de Disco "); 

for(int i = 0 ; i < root.length ; i++){ 
    System.out.println(root[i].toString() + " existe= " + root[i].exists()); 
    if(root[i].exists()==true){ 
     System.out.println("Espacio Total: "+ root[i].getTotalSpace()); 
     System.out.println("Espacio Libre: "+ root[i].getFreeSpace()); 

     String[] listaDeArchivos = root[i].list(); 
     for(String lista:listaDeArchivos){ 
      System.out.println(lista); 
     } 
    } 
} 

,하지만 난 파일 내에서의 모든, 모든 파일을 나열주기를 필요 지금까지 나는이 도달했습니다.

+0

무엇이 당신 질문입니까? 너 무슨 문제있어? –

+0

하드 디스크의 모든 디렉토리 및 파일 이름을 인쇄하고 싶습니다. – user3362366

+0

Welcome to Stack Overflow. 질문을 향상시킬 수 있습니다. [Minimal, Complete, Verifiable example] (http://stackoverflow.com/help/mcve)을 읽어보십시오. 코드가 추가로 아무 것도없는 정확한 문제를 보여줄 때 자원 봉사자를 존중합니다. 또한 가독성을 위해 코드의 서식을 지정하고 스크롤을 제거하십시오. – zhon

답변

0

모든 디렉토리와 각 디렉토리 내의 모든 파일을 나열하려면 재귀 함수를 설계해야합니다.

아래 코드에서이 기능은 listStructure입니다.
전화 당신의 for loop

for(String lista:listaDeArchivos){ 
     System.out.println(lista); 
     listStructure(lista); 
     } 

기능의 내부에서이 기능은 디렉토리 구조를 나열합니다.

public void listStructure(String fileName) 
{ 
File file=new File(fileName); 
if(!file.exists()) 
{ 
System.out.println(fileName+" doesn't exists"); 
return; 
} 
if(!file.isDirectory())//check whether it's a directory or not 
{ 
System.out.println(fileName); 
return; 
} 
String files[]=file.list(); //if it's a directory then iterate through the directory 
for(int i=0;i<files.length;i++) 
{ 
listStructure(fileName+File.separator+files[i]);//recursively calling the function 
} 
} 
관련 문제