我有一个要求是我必须编写一个高性能文件搜索程序,该程序应该列出与从最顶层文件夹开始提供的名称模式匹配的所有文件和文件夹,并在子文件夹中递归搜索。
程序可以是具有以下输入的命令行主类
开始搜索的顶级文件夹。例如 C:\MyFolders 要搜索的项目类型。文件或文件夹或两者 搜索模式 java 正则表达式 (java.util.regex) 被接受为 paatern
例如MFile .tx?将找到应用程序必须返回的 UMFile123.txt 和 AIIMFile.txs 的超时(以秒为单位)。否则它必须返回“无法完成操作”消息。
我想出了另一种方法是..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import com.sapient.test.fileSearch.FileSearch;
public class FilesearchMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int flag=0;
System.out.println("Type Item to Search ");
System.out.println("1 File");
System.out.println("2 Folder ");
System.out.println("3 Both");
System.out.println("0 Exit");
try{
BufferedReader readType = new BufferedReader(new InputStreamReader(System.in));
String searchType =readType.readLine();;
System.out.println("Enter name of file to search ::");
BufferedReader readName = new BufferedReader(new InputStreamReader(System.in));
String fileName=readName.readLine();
if(searchType==null && fileName==null){
throw new Exception("Error Occured::Provide both the input Parameters");
}
validateInputs(searchType,fileName);
FileSearch fileSearch = new FileSearch(searchType,fileName);
List resultList=fileSearch.findFiles();
System.out.println(resultList);
}catch(IOException io){
System.out.println("Error Occured:: Check the input Parameters and try again");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
private static void validateInputs(String searchType, String fileName)
throws Exception{
if(!(searchType.equals("1") || searchType.equals("2") || searchType.equals("3")) ){
throw new Exception("Error:: Item to search can be only 1 or 2 or 3");
}
if(searchType.equals("") || fileName.equals("")){
System.out.println("Error Occured:: Check the input Parameters and try again");
}
}
}
另一个文件是...
public class FileSearch {
private String searchType;
private String fileName;
public FileSearch(){
}
public FileSearch(String sType,String fName){
this.searchType=sType;
this.fileName=fName;
}
public String getSearchType() {
return searchType;
}
public void setSearchType(String searchType) {
this.searchType = searchType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List findFiles(){
File file = new File("C:\\MyFolders");
return searchInDirectory(file);
}
//Assuming that files to search should contain the typed name by the user
//
private List searchInDirectory(File dirName){
List<String> filesList = new ArrayList<String>();
if(dirName.isDirectory()){
File [] listFiles = dirName.listFiles();
for(File searchedFile : listFiles){
if(searchedFile.isFile() && searchedFile.getName().toUpperCase().contains(getFileName().toUpperCase())&&
(getSearchType().equals("1") || getSearchType().equals("3") ) ){
filesList.add(searchedFile.getName());
}else if(searchedFile.isDirectory() && searchedFile.getName().toUpperCase().contains(getFileName().toUpperCase())
&& (getSearchType().equals("2") || getSearchType().equals("3") ) ){
filesList.add(searchedFile.getName());
searchInDirectory(searchedFile);
}else{
searchInDirectory(searchedFile);
}
}
}
return filesList;
}
}
Please advise is this approach is correct as per design..!