• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Finding a file matching a pattern?

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

i am trying to write a method that searches a directory (plus subdirs) for a file and returns true when a file that matches the given pattern was found. otherwise it should return false. the head should look like this:



i have no idea how to do that.

do you have an idea?

THANKS
[ June 08, 2004: Message edited by: Andreas Golchert ]
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to outline some suggested steps. If I name a step that you need technical help with, holla.

create a method that does the following:

1. given the name of a directory, create a File object that points to that directory.

2. list all the files contained in that directory.
2. 1 If a given sub file is not a directory, then check to see if it's the one you want. if yes, return a new File object that points to the target. if not, check the other non-directory files. If the file you're looking for isn't there, return null.

Once you have this done, let me know. The next step will be amazingly simple

HTH,
M
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, thanks for your answer. i will try that within the next days and post back.....

thanks :-)
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, here is what i have now:




i compiled the code with no errors. what do you think?

THANKS :-)
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it looks good . I've rounded it out in the example below, so it searches sub directories also. Notice, this isn't the style I would code it, but I wanted to be consistent with your coding style, so there would be one less barrier.



HTH,
M
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an alternative, you could explore the FilenameFilter class so you only get directories (for recursion) and files matching your pattern back - when File has these filtering facilities in place it seems a bit of a pity not to use them. Although truth be told I can't say the code gets any simpler As a point of style, when you need to turn stuff into a String, consider using toString() or (if the object may be null) String.valueOf(). It's a clearer statement of intent than concatenating with "", and more efficient too.

- Peter
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, thanks to both of you. i think i will try both.

thanks :-)
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IT WORKS...thanks guys
[ June 10, 2004: Message edited by: Andreas Golchert ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic