我有一个要求是我必须编写一个高性能文件搜索程序,该程序应该列出所有文件和文件夹,它们与从最顶层文件夹开始提供的名称模式匹配,并在子文件夹中递归搜索。
程序可以是命令行主类,具有以下输入
开始搜索的顶级文件夹。示例是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..!Please update ..!1
答案 0 :(得分:0)
没有。你说你想要搜索你没有做的正则表达式,因为你只是将它与contains
进行比较。如果这不仅仅是一个简单的练习,您可能需要查看方法File.listFiles()
和Guava's PatternFilenameFilter
。如果你真的想限制程序运行的时间,请看看Guava的SimpleTimeLimiter
。但是,正如Gene在她的评论中所说,如果只需要一个简单的find
类工具,其他工具可能更合适。