Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:59:29+00:00 2026-06-09T09:59:29+00:00

I was having one requirement that is I have to write a high performance

  • 0

I was having one requirement that is I have to write a high performance file search program, the program is supposed to list down all the files and folder that matches the name pattern provided starting from the topmost folder and searching recursively in subfolders.

program can be command line main class with the following input

The top folder to start search . example that is C:\MyFolders
Type of item to search. File or folder or both
Search pattern java regular expression (java.util.regex) are accepted as paatern

example MFile.tx? will find UMFile123.txt and AIIMFile.txs’
Timeout (in secs) by which application must return. otherwise it must return with “could not complete operation ” message.

I have come up with anoter approach that is..

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

        }

    }

}

the other file is…

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..!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:59:31+00:00Added an answer on June 9, 2026 at 9:59 am
     if (topFolderOrFile.isDirectory()) {
          File[] subFoldersAndFileNames = topFolderOrFile.listFiles(fileFilter);
    

    Where fileFilter could look something like this

    public class MyFileFilter implements FileFilter{
    
        public boolean accept(File pathname) {
            return fileNamePattern.matcher(pathname.getName()).find();
        }
    
    }
    

    This basically guarantees that the list of files returned will match the criteria in the FileFilter

    Now, it’s semantics in this case, because in order for the listFiles method to work, it will still need to iterate over all the files anyway.

    You could try maintaining a single instance of the filter, rather then recreating it over each iteration, but you will need to profile the difference between your algorithm and any benefits that this might bring.

    On a side note, you could deploy a Thread queue of some kind, where each thread is responsible for checking the matches of a given directory and queuing any new sub directories. Just a thought

    Reusable Pattern

    public static void searchFile(String topFolderName, String type,
            String fileNamePatternRegExp, long timeOut) throws IOException {
    
        long startTimeStamp = Calendar.getInstance().getTimeInMillis();
    
        File topFolderOrFile = new File(topFolderName);
        Pattern fileNamePattern = Pattern.compile(fileNamePatternRegExp);
    
        searchFile(topFolderName, type, fileNamePattern, long timeOut);
    
    }
    
    public static void searchFile(String topFolderName, String type,
            Pattern fileNamePattern, long timeOut) throws IOException {
        //...
    }
    

    These are the basic changes I’ve made, but really, you have to decide if they work.

    public static class PatternFileFilter implements FileFilter {
    
        private Pattern fileNamePattern;
    
        public PatternFileFilter(Pattern fileNamePattern) {
    
            this.fileNamePattern = fileNamePattern;
    
        }
    
        @Override
        public boolean accept(File pathname) {
    
            return fileNamePattern.matcher(pathname.getName()).find() || pathname.isDirectory();
    
        }
    
        public Pattern getPattern() {
            return fileNamePattern;
        }
    }
    
    public static void searchFile(File topFolderOrFile, String type, PatternFileFilter filter, long timeOut) throws IOException {
    
        long startTimeStamp = Calendar.getInstance().getTimeInMillis();
    
        if (topFolderOrFile.isDirectory()) {
    
            File[] subFoldersAndFileNames = topFolderOrFile.listFiles(filter);
            if (subFoldersAndFileNames != null && subFoldersAndFileNames.length > 0) {
                for (File subFolderOrFile : subFoldersAndFileNames) {
    
                    if (ITEM_TYPE_FILE.equals(type) && subFolderOrFile.isFile()) {
                        System.out.println("File name matched ----- "
                                + subFolderOrFile.getName());
                    }
                    if (ITEM_TYPE_FOLDER.equals(type)
                            && subFolderOrFile.isDirectory() && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
                        System.out.println("Folder name matched ----- "
                                + subFolderOrFile.getName());
                    }
                    if (ITEM_TYPE_FILE_AND_FOLDER.equals(type) && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
                        System.out.println("File or Folder name matched ----- "
                                + subFolderOrFile.getName());
                    }
    
                    // You need to decide if you want to process the folders inline
                    // or after you've processed the file list...
    
                    if (subFolderOrFile.isDirectory()) {
                        long timeElapsed = startTimeStamp
                                - Calendar.getInstance().getTimeInMillis();
                        if (((timeOut * 1000) - timeElapsed) < 0) {
                            System.out
                                    .println("Could not complete operation-- timeout");
                        } else {
                            searchFile(subFolderOrFile, type,
                                    filter, (timeOut * 1000)
                                    - timeElapsed);
                        }
                    }
                }
    
            }
    
        }
    
    }
    
    public static void searchFile(String topFolderName, String type, String fileNamePatternRegExp, long timeOut) throws IOException {
    
        File topFolderOrFile = new File(topFolderName);
        Pattern fileNamePattern = Pattern.compile(fileNamePatternRegExp);
    
        searchFile(topFolderOrFile, type, new PatternFileFilter(fileNamePattern), timeOut);
    
    }
    

    I’d just like to say, here’s a fish, now you need to learn to fish 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a script that finds all executable files in a directory.
I am having one flow field manager added on screen & in that manager
I'm currently having an issue where I have a javascript object that is trying
Ok, so I have a file called functions.ps1 that contains only function code: something
All, I have a requirement to hide my EF implementation behind a Repository. My
I have one requirement as shown below : Suppose we have EMP table with
I have an assignment that has asked me to copy a file using buffered
I'm creating my first Windows Forms Application, and one requirement is that there is
I have a program that uses the registry to save the last 10 files
I have a web application having three pages, one is login page, second is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.