This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to write a reg exp to search for files?  RSS feed

 
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using Java 1.5. In a certain directory, I want to search for the presence of a file, say "a.txt" and if that exists, then create a file "a-1.txt". If there is an "a.txt" and an "a-1.txt" file, I want to create a "a-2.txt" file. Short of doing loops, is there a way I can check if a "a-#.txt" file exists, and what that # is using regular expressions somehow?

Thanks, - Dave
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure you can, there is the java.io.FileFilter interface, provide your own implementation, which requires you create an accept() method, that returns true if the file found is one you want to include in the filtered find file results, or to make it return false if you do not want to see the file.

such as



and then invoke this as

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!