2017-10-31 6 views
0

direcory에있는 하위 폴더에서 (.xxx) 유형의 파일을 읽는 방법을 묻고 싶습니다.Java의 디렉토리 하위 폴더에서 특정 유형의 파일을 읽는 방법

나는 하위 폴더 (하위 폴더의 수를 알 수없는) 파일의

C:\MainFolder 
    contains->   [subFolder_A][subFolder_B]......[subFolder_n] 

모든 하위 폴더 containts 충분히 있지만 그 중 하나는 다른 종류의, 특히 (.fingerprint)가 들어있는 폴더를 유형. \ MainFolder fulled되어 스캔 :

 [subFolder_A] --> a.txt , atitle.txt , afin.fingerprint 
     [subFolder_B] --> b.txt , btitle.txt , bfin.fingerprint 
      ............................................... 
     [subFolder_n] --> n.txt , ntitle.txt , nfin.fingerprint 

그래서 내가 ... 때마다 자바에이 파일 중 하나를 읽을 일부 기능 다음 C까지 다음 하나를 수행합니다.

내가 코드에 직접 경로를 주면 내가 하나를 읽을 수 있지만 사람이 자동 방식

 Path path = Paths.get("C:\\MainFolder\\afin.fingerprint"); 
     byte[] data; 
     byte[] fin; 
     try { 
      this.data = Files.readAllBytes(path); 
      byte[] data = Files.readAllBytes(path); 
     } catch (Exception e) {....} 
      fin=data; 

답변

3

당신은 walk the file tree recursively with a stream 수와 그들 모두를 데리고 알고 있다면 내가 몇 가지 팁을 원하는이 같은 예 :

Files.walk(Paths.get("C:/MainFolder")) 
     .filter(p -> p.toFile().isFile()) 
     .filter(p -> p.getFileName().toString().endsWith(".fingerprint")) 
     .forEach(p -> process(p)); 
+0

죄송는'getFileName' - 그리고 과정은 당신이 작성해야하는 방법입니다! – assylias

+0

{ Files.walk (Paths.get ("C : \ MainFolder")) .filter (p -> p.toFile(). isFile()) .filter (p -> p.getFileName .endsWith ("지문")) .forEach (p -> process (p)); } catch (IOException ex) { Logger.getLogger (main.class.getName()) .log (Level.SEVERE, null, ex); } } static void 프로세스 (경로 p) { System.out.println ("파일을 찾음"+ p.getName); } 뭔가 그 일을 좋아하지 않아 .. 실제로 인쇄 대신 내가 함수를 호출합니다 (인쇄가 발견되면 볼 수있다) –

+0

무슨 일을하지 않았습니까? – assylias

1

를 사용하여이 같은 서브 디렉토리를 통해 루프 할 수 있습니다

File directory = new File(//path to directory); 
    for(File subdirectory : directory.listFiles()){ 
    for(File file : Subdirectory.listFiles()){ 
     if(file.toString().endsWith(".footprint"){ 
     //doStuff 
    } 
    } 
} 
+0

안녕하세요, Nakos, 나는 주 디렉토리에서 시작할 코드를 편집했습니다. 이 상황에서해야 할 일은 디렉토리 하위 디렉토리에 대한 첫 번째 경로를 제공하는 것입니다. 희망이 도움이됩니다. – Sam

관련 문제