• 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

guide for getting .txt format file

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

I have written a code using FileFilter interface to extract all the file that has extension of .txt, please have a look




So, I am uncomfortable with accept method of FileFilter interface , the output of this program is displaying all the directives, instead of extension txt file.
what other method I should have to write in accept method for displaying the same.

Jesper: Added code tags
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First: please use code tags, it makes reading your code example a lot easier.

Second: your accept() method returns true if the file it is checking is a directory, so why do you expect it would do anything else? If you want to check if you have a file with the extension ".txt", then have the accept() method check for that (hint: look at the String method endsWith() )

Third: be precise in the naming of your variables. The accept() method receives a File as parameter, so why do you call that parameter "pathname"? To another reader looking at your code, a variable named "pathname" might suggest that a string is passed here.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D. Ogranos wrote:Third: be precise in the naming of your variables. The accept() method receives a File as parameter, so why do you call that parameter "pathname"?


Actually, that's what it's called in the FileFilter class - I suspect to indicate the fact that its primary use is for filtering pathnames.

However, I agree that it's not the best name, for the exact example shown. There's nothing that says it has to filter names.

Winston
 
vinayGuddu Pandey
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston Gutkowski,

Actually I want to know that by using FileFilter interface is it possible to extract the format of file(as I want) by using the accept method in FileFilter interface.
As we have an option in FilenameFilter's accept method .
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The FileFilter interface is almost exactly the same, and has the same purpose, as the FilenameFilter interface.

If you want to list only files that have the extension ".txt", then you have to write your accept() method so that it returns true if pathname ends with ".txt", and false otherwise. It's really simple - try it and show us what you've tried.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

D. Ogranos wrote:Third: be precise in the naming of your variables. The accept() method receives a File as parameter, so why do you call that parameter "pathname"?


Actually, that's what it's called in the FileFilter class - I suspect to indicate the fact that its primary use is for filtering pathnames.



Hmm, you're right...ok in this case, sorry vinayGuddu Pandey, I should've looked at that API first
reply
    Bookmark Topic Watch Topic
  • New Topic