• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Filtering Files

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I want to select files from the path specified and as per the file naming pattern provided, for eg. findFiles("c:\fun","*.*") should print all files in the directory c:\fun. Can anyone illustrate an example to accomplish this.

Thanks in advance.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the FilenameFilter interface.
 
ch praveen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Mani,

I know about FileNameFilter, which is an interface. Actually I want to apply a pattern like *.*, *java* on filenames in accept() method of FileNameFilter. I have included java.util.regexp package to accomplish this, but Iam getting an exception as follows.

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*str
^
at java.util.regex.Pattern.error(Pattern.java:1489)
at java.util.regex.Pattern.sequence(Pattern.java:1606)
at java.util.regex.Pattern.expr(Pattern.java:1506)
at java.util.regex.Pattern.compile(Pattern.java:1274)
at java.util.regex.Pattern.<init>(Pattern.java:1030)
at java.util.regex.Pattern.compile(Pattern.java:777)
at ListFiles$1.accept(ListFiles.java:19)
at java.io.File.list(File.java:951)
at ListFiles.printList(ListFiles.java:24)
at ListFiles.toString(ListFiles.java:58)
at java.lang.String.valueOf(String.java:2177)
at java.io.PrintStream.print(PrintStream.java:462)
at java.io.PrintStream.println(PrintStream.java:599)
at ListFiles.main(ListFiles.java:75)

As Iam not familiar with regexp package, can anyone suggest how to filter files with pattern *.*, *java*.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regex seems like the way to go. However, the syntax for regexes is different from what you are used to with file matching. For instance, the * in a regex means to match 0 or more of the PREVIOUS CHARACTER in the regex. So "a*" matches zero or more a's. You are getting an error because there is no preceding character. To match any character, you use . (dot). Note that . has special meaning, so to match a literal . you need to escape it with \. But \ has special meaning in a Java string you need to escape it. To put this altogether in a coerent example, a regular expression that is equivalent to *.java will be ".*\\.java".

Make sense?

Layne
[ October 21, 2005: Message edited by: Layne Lund ]
 
ch praveen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Layne,

Escaping special characters in the pattern and including . at beginning of pattern is the solution. Thanks for your reply.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad I could help. Feel free to come back with more questions.

Keep Coding!

Layne
 
reply
    Bookmark Topic Watch Topic
  • New Topic