aspose file tools
The moose likes Java in General and the fly likes File Exists Wildcard Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "File Exists Wildcard" Watch "File Exists Wildcard" New topic
Author

File Exists Wildcard

Mark Mero
Greenhorn

Joined: Oct 14, 2004
Posts: 12
I have written a piece of code that creates a new file object when passed the full path & filename. It then checks if the file exists. No problem.

File f = new File(location + localname);

if (tj.isReceived()==false){
if (f.exists()){
System.out.println(location + localname + " received.");
//Do other stuff
}

The problem i have is that i want to check if a file exists using a wildcard e.g. check if c:\filex123ABC.txt exists merely by passing C:\file123

Do you have to implement FilenameFilter in order to achieve this & how simple is it to convert the existing code?

Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

Hi Mark,

Implementing this as a FilenameFilter would be very easy, something like



You could also easily write one that used regular expressions for matching.


[Jess in Action][AskingGoodQuestions]
Mark Mero
Greenhorn

Joined: Oct 14, 2004
Posts: 12
Thanks Ernest,

How would i add this to my existing class.

Regards
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Mark, you'll need to get the directory from your wildcard spec. Something like a substring from 0 to the last position of file separator. Then create a File on that directory and call the list() method with the filter shown above as an argument. All files returned by list() will match your pattern. Cool beans.

BTW: That's all deliberately vague so you can have the thrill of digging through the JavaDoc. Holler if you need more detail.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mark Mero
Greenhorn

Joined: Oct 14, 2004
Posts: 12
I have tried the FilelistFilter method, but i want to be able to create a file object without having to list the dir contents. What would the syntax be for creating a file object using regular expressions?

Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Think you're out of luck. If you created a file with a wildcard in the name what would its name be?
Mark Mero
Greenhorn

Joined: Oct 14, 2004
Posts: 12
Stan, you're right!
I've modified my use of the FilenameFilter & it is now working fine.

Thanks everyone for all of the assistance.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: File Exists Wildcard
 
Similar Threads
creating file relative to root of web application in windows OS
renameTo is not working
Reading In A Resource Folder
XML handler --> populate Hashmap
how to search and return the existence of a file of a particular type on a path