Jump to content

Java Search Code


trustme
 Share

Recommended Posts

Hello,

My Java teacher insists that I create a program to search for files on the computer based on file type (e.g.  .rar, .zip) after I made a sarcastic comment about the intelligence of the class.  (They did not know the difference between a local and global variable by the end of the year, it is a beginners class but…)  So the threat was that he would take off my extra credit points (I did not do a final project because I had more extra credit than the final was worth).

I was looking around online and found out that I should be using some extension of file.  Also, there was a mention of a memory overflow when trying to search the entire computer.  Since this is the coding forum and you guys are all proficient in numerous languages I was wondering if I could get some help?  This is probably way over my head; the final test was as easy as print lines and calling methods.  I’m pretty confident in my ability to add the files and build a gui in swing with the paint command, but the actual code for searching escapes me.

Thanks for any help.

Ryan

Link to comment
Share on other sites

You could have it search for files based on it's extension. But that is actually the bad (and expected) way. Instead you should try and match files to a signature. For example exes (on windows, not DOS) always contain the phrase "This program cannot be run in DOS mode.". So you can a simple text search through the beginning of the file.

Figuring out if a plain text file is actually a plaint text will be the hardest thing. All you can rely on there is the file exstension and that it contains valid ACSII or Unicode characters.

Link to comment
Share on other sites

I understand there may be a "better" way to do it, I don't really care how it works as long as it does.  Doing what you said is probably even more way over my head.  If you think its easier to do it this way, I'd love to hear how.

Ryan

Link to comment
Share on other sites

public class DirectoryOrMatchingExtensionFilter extends FileFilter {

    private String extension;
    private boolean ignoreCase;

    public ExtensionFilter(String extension, boolean ignoreCase) {
        this.extension = extension;
        this.ignoreCase = ignoreCase;
    }

    public boolean accept(File target) {
        if (target.isDirectory()) {
            return true;
        } else {
            String targetName = target.getName();
            String targetExtension = targetName.substring( targetName.lastIndexOf('.')+1 );
            if (ignoreCase) {
                return targetExtension.equalsIgnoreCase( extension );
            } else {
                return targetExtension.equals( extension );
            }
        }
    }
}

public List<File> findFilesWithExtension(File directory, String extension, boolean ignoreCase) {
    DirectoryOrMatchingExtensionFilter filter = new DirectoryOrMatchingExtensionFilter(extension,ignoreCase);
    return findFiles(directory,filter);
}

public List<File> findFiles(File directory, FileFilter filter) {
    List<File> result = new ArrayList<File>();
    List<File> matches = Arrays.asList( directory.listFiles(filter) );
    for (File match : matches) {
        if (match.isDirectory()) {
            // Recurse into this directory.
            result.addAll( findFiles(match,filter) );
        } else {
            result.add( match );
        }
    }
}

Yawn.

Link to comment
Share on other sites

When there is no extension, lastIndexOf returns -1. Because I always add 1 (to jump over the found dot) that would mean I'm doing a substring(0) on the target name which gives simply a copy of target name. The equals following it still works.

The listFiles method can potentially call a SecurityException, but other than that things should be safe. Have to admit though that I  didn't even bother trying to compile this, so I might just be missing a try/catch here or there.

Link to comment
Share on other sites

talking about identifying a file. An interesting aside:

I actually tried to do this a while back, found it quite difficult. I'm talking about identifying a file (*not* by its extension). It's quite difficult, there is a program that runs on UNIX/UNIX-LIKE platforms does it though, 'file'. I have a feeling it does something like reading the first  few bytes or so. It's one of those thing where one would think it would be quite easy to do..but when you think about it..

must check the source code of that program.

first post in a while

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...