Extension Filename Filter : FileFilter « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » FileFilterScreenshots 
Extension Filename Filter
    
/**
 * The utillib library.
 * More information is available at http://www.jinchess.com/.
 * Copyright (C) 2002 Alexander Maryanovsky.
 * All rights reserved.
 *
 * The utillib library is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The utillib library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


import java.io.File;
import java.io.FilenameFilter;


/**
 * An implementation of the <code>java.io.FilenameFilter</code> which accepts
 * files whose name ends with a certain string. This makes it useful for
 * accepting files with a certain extension.
 */


public class ExtensionFilenameFilter implements FilenameFilter{


  /**
   * The strings with one of which a file's name must end in order to pass the 
   * filter.
   */

  private final String [] endStrings;




  /**
   * Creates a new ExtensionFilenameFilter with the given string. Only files
   * ending with that string will be accepted. Note that in order to use this
   * class to accept only files with a certain extension, you must also provide
   * the '.' character before the extension. For example, to accept only 'txt'
   * files, you must pass ".txt".
   */

  public ExtensionFilenameFilter(String endString){
    this(new String[]{endString});
  }




  /**
   * Creates a new ExtensionFilenameFilter with the given string array. Only
   * files ending with one of the strings in the given string array will be
   * accepted by the created ExtensionFilenameFilter.  Note that in order to use
   * this class to accept only files with a certain extension, you must also 
   * provide the '.' character before the extension. For example, to accept 
   * only 'txt' files, you must pass ".txt".
   */

  public ExtensionFilenameFilter(String [] endStrings){
    this.endStrings = new String[endStrings.length];
    for (int i=0;i<endStrings.length;i++){
      this.endStrings[i= endStrings[i];
    }
  }




  /**
   * Tests whether the specified file passes the filter. Returns true if the
   * file's name ends with one of the string specified in the constructor.
   */

  public boolean accept(File dir, String filename){
    for (int i=0;i<endStrings.length;i++){
      if (filename.endsWith(endStrings[i]))
        return true;
    }
    return false;
  }


}

   
    
    
    
  
Related examples in the same category
1. Filter files by name
2. Filter by file
3. List files of a certain type
4. Filters files based on the extension (what the filename ends with)
5. A prefix based filename filter
6. File filter: retrieve at most predefined number of files which are older than specified data
7. A suffix based file filter.
8. A filesystem filter.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.