Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

guide for getting .txt format file  RSS feed

 
vinayGuddu Pandey
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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 .
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!