2016-11-11 2 views
2

시작 경로로 표시되는 Java 파일을 찾기 위해 재귀 프로그램을 작성하는 방법을 궁금합니다. 지정된 파일에 대한 트리를 검색해야합니다. 파일이 발견되면 파일의 위치가 리턴되어야합니다. 이것은 내가 지금까지 가지고있는 것 (많지는 않지만 여전히 기본적인 청소가 필요하다)이다. 나는이 정확한 방법을 사용할 필요가있다. 나는 주로 어떤 방법을 사용하는지와 혼동합니다. 그래서 내가 사용해야 알고Java에서 파일을 재귀 적으로 찾는 방법은 무엇입니까?

File f = new File(dirName); 

String [] fileList = f.list(); 

File aFile = new File (dirName + "\\" + fileList[i]); 

if (aFile.isDirectory()) {...} 

public class FindFile { 

것은 당신이 날 그 각각의 놀라운 도움이 될 것이라고에서 어떤 일이 일어나는지 방법을 알아내는 데 도움 수 있다면! 각 메서드의 논리를 실제로 이해하지 못합니다. 나 또한 활용할 필요가있는 다른 클래스의 운전사가 있습니다. 발견하는 경우

/** 
* This constructor accepts the maximum number of files to find. 
*/ 
public FindFile (int maxFiles) 
{ 
} 

/** 
* The parameters are the target file name to look for and the directory to start in. 
* @param target = target file name, dirName = directory to start in 
*/ 
public void directorySearch (String target, String dirName) { 
    File f = new File(dirName); 
    String [] fileList = f.list(); 
    File aFile = new File(dirName + "\\" + fileList[i]); 
    if (aFile.isDirectory()) { 
    } 
    else { 
    } 
} 

/** 
* This accessor returns the number of matching files found. 
* @return number of matching files found 
*/ 
public int getCount() { 
    return -1; 
} 

/** 
* This getter returns the array of file locations, up to maxFiles in size. 
* @return array of file locations 
*/ 
public String [] getFiles() { 
    return new String[] {""}; 
} 

/** 
* Prompt the user for max number of files to look for, the directory to start in, and the file name. 
* Then, print out the list of found files with the full path name to the file (including starting 
* directory). In the event of an exception being thrown, driver catches it and provides an appropriate 
* message to the user. 
*/ 
public static void main (String [] args) { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("What is the max number of files to look for?"); 
    System.out.println("What directory should we start in?"); 
    Systme.out.println("What is the file name?"); 
    } 

}

답변

2

당신은 모든 파일, 디렉토리 및 하위 디렉토리

public static void main(String[] args) { 
    boolean found = searchFile(new File("/tmp"), "10174"); 
    System.out.println(found); 
} 

private static boolean searchFile(File file, String search) { 
    if (file.isDirectory()) { 
     File[] files = file.listFiles(); 
     for (File f : files) { 
      boolean found = searchFile(f, search); 
      if (found) 
       return true; 
     } 
    } else { 
     if (search.equals(file.getName())) { 
      return true; 
     } 
    } 
    return false; 
} 

파일이 필요로하는 경우에 반환 할를 파일을 검색 할 수 재귀를 사용할 필요가

static File searchFile(File file, String search) { 
    if (file.isDirectory()) { 
     File[] arr = file.listFiles(); 
     for (File f : arr) { 
      File found = searchFile(f, search); 
      if (found != null) 
       return found; 
     } 
    } else { 
     if (file.getName().equals(search)) { 
      return file; 
     } 
    } 
    return null; 
} 
+0

부울 대신 발견 된 파일 (또는 null)을 반환하도록 변경해야합니다. – Bill

+1

@Bill 예, 업데이트 된 답변 – Saravana

+0

설명이 업데이트되었습니다. – iloveprogramming

8

다음과 같은 java 8 람다 기능을 사용할 수 있습니다

Files.walk(Paths.get("your search path")) 
     .filter(Files::isRegularFile) 
     .forEach((f)->{ 
      String file = f.toString(); 
      if(file.endsWith("file to be searched")) 
       System.out.println(file + " found!");    
     }); 
관련 문제