File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about FilenameFilter

 
kartik krishnan
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays.sort() would probably be a good starting point for that...
[ April 24, 2008: Message edited by: Jim Yingst ]
 
Rob Spoor
Sheriff
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For which you need a comparator if alphabetical sorting is not what you need.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic