File Searching Algorythm

Hello, Recently I have created a file searching algorithm. I do not have all the skills required to make a library, so I will post my code: Syntax for the class is included below: Class Name: fileSearch

Constructors:

fileSearch() :: Default Constructor fileSearch(ArrayList SearchLocations, ArrayList Extensions, ArrayList ExclusionLocations) fileSearch(ArrayList SearchLocations, ArrayList Extensions) fileSearch(ArrayList SearchLocations)

Methoods:

addLocationS(File Location) ::This method adds a location to be searched. Ex:

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File(<your path here>));

You may add as many locations as you would like.

remLocationS(File Location) :: This method removes a location IF it is has been added using above method. Ex:

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File(<your path here>));
searcher.remLocationS(new File(<same path here>));

clrLocationS() :: This will clear all locations added using addLocationS(File Location).

addLocationE(File Location) :: This method adds a location to be excluded in your search. The algorithm will not search below this folder UNLESS you add a specific location under it using addLocationS(File Location). Ex:

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File(<your path here>));
searcher.addLocationE(new File(<your path here>/<location>));

you may add as many locations as you would like.

remLocationE(File Location) :: This method removes an excluded location. Ex:

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File(<your path here>));
searcher.addLocationE(new File(<your path here>/<location>));
searcher.remLocationE(new File(<same path here>));

clrLocationE() :: This will clear all locations added using addLocationE(File Location).

addExtension(String extension) :: This will restrict files to having a specific extension. Ex:

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File(<your path here>));
searcher.addExtension(".jpg"); //This will find all .jpg files under the location above.

If you have not added any extensions, all files that are found will be returned.

remExtension(String extension) :: This will remove an extension if it has been added with above method.

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File(<your path here>));
searcher.addExtension(".jpg");
searcher.remExtension(".jpg");

clrExtension() :: This clears all Extension Filters

clearResults() :: This will clear the list of found files.

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File("C:/"));
searcher.search();
searcher.clearResults();

search() :: This is how you actually search for files

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File("C:/"));
searcher.search();

results() :: This returns an ArrayList

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File("C:/"));
searcher.search();
ArrayList<File> returned = searcher.results();

getInfo() :: This method searches through the current results and tallies the various extensions. It returns a ArrayList that looks like this:

.jpg 2
.pdf 5
.docx 10
...

Example:

fileSearch searcher = new fileSearch();
searcher.addLocationS(new File("C:/"));
searcher.search();
ArrayList<File> returned = searcher.results();
ArrayList<String> info = searcher.getInfo();

I welcome coments, questions, suggestions, and help finding any bugs. Please feel free to contact me at kaigrid@gmail.com. The Code is included below.

/*
 * CLASS: fileSearch
 * VERSION: 1.0
 * COPYRIGHT 2014
 * THE CODE INCLUDED IN THE fileSearch CLASS ARE HEREBY PROPERTY OF TAYLOR A FOOTE.
 * THE CODE INCLUDED CAN BE SHARED AS LONG AS CREDIT IS GIVEN TO TAYLOR A FOTE.
 * |   /     /-----\         /-------\  /----\      |------\
 * |  /     /       \   |   /           |    |  |   |       \
 * | /      |       |       |           |    |      |       |
 * |<       |       |   |   |           |----/  |   |       |
 * | \      |-------|   |   |     ---\  |\      |   |       |
 * |  \     |       |   |   |        |  | \     |   |      /
 * |   \    |       |   |   \________/  |  \    |   \-----/
 *
 */

import java.io.File;
import java.util.ArrayList;

public class fileSearch {
    private static ArrayList<File> searcpaths;
    private static ArrayList<File> exclusions;
    private static ArrayList<String> extensions;
    private static ArrayList<File> files;
    //Default Constructor No Search Paths, Exclusion Paths, or Extension Filters
    public fileSearch() {
        searcpaths = new ArrayList<File>();
        exclusions = new ArrayList<File>();
        extensions = new ArrayList<String>();
        files = new ArrayList<File>();
    }
    //Constructor that Adds Search Paths, Extension Filters, and Exclusions
    public fileSearch(ArrayList<File> SearchLocations, ArrayList<String> Extensions, ArrayList<File> ExclusionLocations) {
        searcpaths = SearchLocations;
        exclusions = ExclusionLocations;
        extensions = Extensions;
        files = new ArrayList<File>();
    }
    //Constructor that ONLY adds Search Paths, and Extension Filters.
    public fileSearch(ArrayList<File> SearchLocations, ArrayList<String> Extensions) {
        searcpaths = SearchLocations;
        exclusions = new ArrayList<File>();
        extensions = Extensions;
        files = new ArrayList<File>();
    }
    //Constructor that ONLY Adds Search Locations
    public fileSearch(ArrayList<File> SearchLocations) {
        searcpaths = SearchLocations;
        exclusions = new ArrayList<File>();
        extensions = new ArrayList<String>();
        files = new ArrayList<File>();
    }
    //Returns the Results of the Search
    @SuppressWarnings("unchecked")
    public ArrayList<File> results() {
        return (ArrayList<File>) files.clone();
    }   
    //Add Location to Be Searched
    public void addLocationS(File Location) {
        searcpaths.add(Location);
    }
    //Remove Location if it is in search path
    public void remLocationS(File Location) {
        if(searcpaths.contains(Location)) {
            searcpaths.remove(Location);
        }
    }
    //Clear Search Paths
    public void clrLocationS() {
        searcpaths.clear();
    }
    //Add Folder Exclusion (DOES NOT WORK FOR FILES)
    public void addLocationE(File Location) {
        exclusions.add(Location);
    }
    //Remove Folder Exclusion If it is present
    public void remLocationE(File Location) {
        if(exclusions.contains(Location)) {
            exclusions.remove(Location);
        }
    }
    //Clear Exclusion Paths
    public void clrLocationE() {
        exclusions.clear();
    }
    //Add File Extension Filter
    public void addExtension(String extension) {
        extensions.add(extension);
    }
    //Remove File Extension Filter if Present
    public void remExtension(String extension) {
        if(exclusions.contains(extension)) {
            exclusions.remove(extension);
        }
    }
    //Clear Extension Filters
    public void clrExtension() {
        extensions.clear();
    }
    //Clears Results
    public void clearResults() {
        files.clear();
    }
    //Runs Search
    public void search() {
        File file;
        File[] listed;
        files.clear();
        ArrayList<File> folders = new ArrayList<File>();
        try {
            for(int x = 0; x < searcpaths.size(); x++) {
                file = searcpaths.get(x);
                if(file.isDirectory()) {                            
                    if(notExcluded(file)) {
                        folders.add(file);
                    }
                } else {
                    if(extensions.size() == 0) {
                        System.out.println("Adding File 1");
                        files.add(file);
                    } else {
                        //System.out.println("Checking Extension");
                        for(int x1 = 0; x1 < extensions.size(); x1++) {
                            if(extensions.get(x1).equals("")) {
                            } else {
                                if(file.toString().substring(file.toString().lastIndexOf(".")).equals(extensions.get(x1))) {
                                    //System.out.println("Adding File 2");
                                    //System.out.println(file.toString().substring(file.toString().lastIndexOf(".")) + " = " + extension[x1]);
                                    files.add(file);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("ERROR 1 PLEASE CONTACT DEVELOPER AND INCLUDE THIS MESSAGE: " + e);
        }
        while(folders.size() > 0) {
            try {
                //System.out.println("Number of Folders: " + folders.size());
                file = folders.remove(0);
                //System.out.println("Current File: " + file.toString());
                listed = file.listFiles();
                for(int x = 0; listed != null && x < listed.length; x++) {
                    file = listed[x];
                    if(file.isDirectory()) {                            
                        if(notExcluded(file)) {
                            folders.add(file);
                        }
                    } else {
                        if(extensions.size() == 0) {
                            //System.out.println("Adding File 1");
                            files.add(file);
                        } else {
                            //System.out.println("Checking Extension");
                            for(int x1 = 0; x1 < extensions.size(); x1++) {
                                if(extensions.get(x1).equals("")) {
                                } else {
                                    if(file.toString().substring(file.toString().lastIndexOf(".")).equals(extensions.get(x1))) {
                                        //System.out.println("Adding File 2");
                                        //System.out.println(file.toString().substring(file.toString().lastIndexOf(".")) + " = " + extension[x1]);
                                        files.add(file);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println("ERROR 2 PLEASE CONTACT DEVELOPER AND INCLUDE THIS MESSAGE: " + e);
            }
        }
    }
    //This function checks if a folders is excluded. Don't Touch.
    private boolean notExcluded(File x) {
        if(exclusions.contains(x)) {
            //System.out.println("EXCLUDE!!");
            return false;
        }
        return true;
    }
    //Gets Extension Demographic Info (How many of Each Extension there Are in the Last Search.
    //This returns the Quantaty of Present File Extensions
    public ArrayList<String> getInfo() {
        int z = 0;
        String infopart;
        ArrayList<String> info = new ArrayList<String>();
        ArrayList<Integer> infonum = new ArrayList<Integer>();
        //System.out.println("getInfo() " + files.size());
        for(int x = 0; x < files.size(); x++) {
            //System.out.println("" + x + "/" + files.size());
            File file = files.get(x);
            try {
                if(info.contains(file.toString().substring(file.toString().lastIndexOf(".")))) {
                    for(int y = 0; y < info.size(); y++) {
                        //System.out.println("Finding Extension");
                        if(info.get(y).equals(file.toString().substring(file.toString().lastIndexOf(".")))) {
                            //System.out.print("Extension Found! Before: " + infonum.get(y) + " After: ");
                            z = infonum.get(y);
                            z++;
                            //System.out.println(z);
                            infonum.set(y, z);
                        }
                    }
                } else {
                    if(file.toString().lastIndexOf(".") > file.toString().lastIndexOf((char) 92)) {
                        //System.out.println("Adding New Entry");
                        info.add(file.toString().substring(file.toString().lastIndexOf(".")));
                        infonum.add(1);
                    }
                }
            } catch(Exception e) {
                System.out.println("Error 3. This may be a file without an extension: " + file.toString());
            }
        }
        for(int x = 0; x < info.size(); x++) {
            z = infonum.get(x);
            infopart = info.get(x);
            info.set(x, (infopart + ": " + z));
        }
        return info;
    }
}

Comments

Sign In or Register to comment.