File filter: retrieve at most predefined number of files which are older than specified data : 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 
File filter: retrieve at most predefined number of files which are older than specified data
    

/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 
 * Project: OpenSubsystems
 
 * $Id: MaximumFileFilter.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 
 * This program 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 General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; 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;
import java.util.Date;

/**
 * File filter implementation to retrieve at most predefined number of files
 * which are older than specified data
 
 @version $Id: MaximumFileFilter.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
 @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
 */
public class MaximumFileFilter implements FilenameFilter
{
   // Attributes ///////////////////////////////////////////////////////////////
   
   /**
    * Current number of files in list
    */
   protected int m_currentFileCount = 0;
   
   /**
    * Maximum number of files in output list
    */
   
   protected int m_iMaximum = 0;
   
   /**
    * Last modification time of file have to be before this date, if it is null, 
    * modification date is not checked
    */
   protected Date m_dtOlderThan;
   
   // Constructors /////////////////////////////////////////////////////////////
   
   /**
    * Constructor with maximum count of file in the output list.
    *  
    @param iMax - maximum number of files in output list. If it is 0 length of 
    @param dtOlderThen  - file last modification time heve to by before this 
    *                       date, if it is null modification time is not checked
    *                       output list is unlimited.
    */
   
   public MaximumFileFilter(
      int  iMax,
      Date dtOlderThen
   )
   {
      m_iMaximum = iMax;
      m_dtOlderThan = dtOlderThen; 
   }
   
   /**
    * {@inheritDoc}
    */
   public boolean accept(
      File   dir, 
      String name
   )
   {
      File currentFile = new File(dir, name);
      
      if (currentFile.isDirectory())
      {
         return false;
      }
      if ((m_dtOlderThan != null&& (currentFile.lastModified() > m_dtOlderThan.getTime()))
      {
         return false;
      }
      if ((m_iMaximum == 0|| (m_currentFileCount <= m_iMaximum))
      {
         m_currentFileCount++;
      }
      return  m_currentFileCount <= m_iMaximum;
   }
}

   
    
    
    
  
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. Extension Filename Filter
6. A prefix based filename filter
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.