• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Wildcard file searches

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know how to do wildcard file searches in Java? For example if the user input in*.java, or *.java ...
Thanks,
Rob
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this thread
http://www.javaranch.com/ubb/Forum38/HTML/000545.html
There is some code for searching down through directories for filename patterns.
James.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and if you can use SDK 1.4, it includes a nifty regular expressions package which can make things easier for you.
 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wish i could use JDK 1.4 but it's still beta, i looked at the sample code, and I can already do what it is doing, i need to wildcard the searches for files though, I did not see how they were doing that in the example thread.
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I am missing your point, but doesn't this code do want you are asking (ie. it returns *.txt and *.class files) ?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume Rob wants the user to be able to type in something like "foo*.java" and find all files that begin with "foo" and end in ".java". Probably the best plan is to use an existing Java pattern matching library - I believe a lot of folks use OROMatcher. Google "java regular expressions" for others.
Or, you can write your own wildcard matcher if you impose a few restictions to keep it simple - like allow only one * in the string, and no other special characters. Then when the user types "foo*.java", use String indexOf('*') to identify the part before the wildcard ("foo") and the part after (".java"). Then reject any filename that doesn't start with "foo" and end with ".java".
 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim, exactly right. Sorry I was not clear enough.
Thanks again.
Rob
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa, this thread has a high wheel reinvention index. The way to list filenames conforming to a certain pattern is to instantiate a java.io.FileFilter (or FilenameFilter) and stick it in File.listFiles() and friends.
Unfortunately there's no wildcard-pattern-matching filter implementation, but it's not that hard to roll your own. Especially not if you use Oro or JDK 1.4 regexp.
In a Swing GUI, you'd use a JFileChooser in combination with a javax.swing.filechooser.FileFilter (G-d only knows why this is an object, and why Sun had to invent three slightly different ways to do one and the same thing in the first place).
- Peter
[This message has been edited by Peter den Haan (edited November 08, 2001).]
 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a filenamefilter it filters file extensions but as mentioned I need to do stuff like this rob*.java, but by using the string methods u can probaly do it.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter- sounds familiar. The only piece missing (maybe) is that a "file search" may well mean you need to search through subdirectories as well. Assuming this is the case, we need a recursive method such as what James Swan posted, since listFiles() does not go into subdirectories. Given that you need to iterate through every File in a directory to see if it's another directory, there isn't really a use for FileFilter or its cousins in this case. You can use it anyway, but it doesn't really buy you anything in this case, IMO.
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Jim pointed out, that's pretty much the motivation behind that piece of code, to be able to recurse through subdirectories, unfortunately you have to hand code a "filename filter".
 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again guys, I used ORO, and it rocks!!
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic