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.
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.
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
posted 7 years ago
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 .
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.