Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Question about FilenameFilter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about FilenameFilter" Watch "Question about FilenameFilter" New topic
Author

Question about FilenameFilter

kartik krishnan
Ranch Hand

Joined: Nov 19, 2006
Posts: 63
Hi,

I am using FilenameFilter to return an array of pdf files.

When I use a File#listFile(new FilenameFilter() {
public boolean accept(File file, String name) {
return name.endwith(".pdf");
}
})

I get an array of File objects. I wanted to know if there is a way to basically manage the order they appear in an array. For example, if i have files a.pdf, b.pdf and c.pdf, is there a way to return an array of files in this order [b.pdf, c.pdf and a.pdf] if this is not the order that the FilenameFilter returns the file objects in.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Kartik,

FilenameFilter only determines if a file should be in the list.

Per the API, File#listFiles(FilenameFilter) behaves the same as listFiles(), only it only returns those abstract pathnames that match the filter.

File#listFiles() explicitly states there is no guarantee of the order that files will be returned.

If you need the files in a specific order, you'll have to write your own method for manipulating the File[] array returned by listFiles(FilenameFilter).
[ April 24, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Arrays.sort() would probably be a good starting point for that...
[ April 24, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

For which you need a comparator if alphabetical sorting is not what you need.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about FilenameFilter