2016-11-11 25 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

應該將其更改爲返回找到的File(或null)而不是布爾值。 – Bill

+1

@Bill是的,更新的答案 – Saravana

+0

更新後的描述。 – iloveprogramming

8

您可以使用像這樣的java 8 lambda功能

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!");    
     });