trustme Posted June 9, 2007 Share Posted June 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
Sparda Posted June 9, 2007 Share Posted June 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
trustme Posted June 10, 2007 Author Share Posted June 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
cooper Posted June 10, 2007 Share Posted June 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
trustme Posted June 10, 2007 Author Share Posted June 10, 2007 Thanks a lot. I'll take a look when I get back later. At least I recognize most of what's there in structure. :-) Quote Link to comment Share on other sites More sharing options...
killzone Posted June 12, 2007 Share Posted June 12, 2007 Nice Java-fu coop, you beet me to it.....is there a need for exception handling here, like if it comes across an ext that it doesn't recognize as an ext >? Maybe not as your ignore case may be enough. Quote Link to comment Share on other sites More sharing options...
cooper Posted June 13, 2007 Share Posted June 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
wetelectric Posted June 14, 2007 Share Posted June 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.