java2db

How to get file names having certain extension or suffix



Sometimes it may require to collect same type of files or file names. The better way to get file names having certain extension or suffix (ex: .txt , .png etc) use the methods listFiles(FilenameFilter filter) or list(FilenameFilter filter)from java.io.Fiel class.

To get file names having certain extension
It requires to implement our class form FilenameFilter interface or implement the FilenameFilter methods as an inner class
Override the accept method for filter according to our requirement

Syntax:

<fileObjectName>.list(<filenameFilterObject>);

Example Code:

Execution Result:

Conclusion :

If the given filter is null then it will return all filenames in the specified directory.
list method internally calls the accept method to filter all files in the directory and finally returns array     of same filenames.
In the same way we can get same filename by using listFiles method which will return File array




Related Posts :

No related posts found!

   [will not be published]



^ <