001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io.filefilter; 018 019import java.io.File; 020import java.io.Serializable; 021import java.nio.file.FileVisitResult; 022import java.nio.file.Files; 023import java.nio.file.Path; 024import java.nio.file.attribute.BasicFileAttributes; 025 026/** 027 * This filter accepts {@code File}s that are files (not directories). 028 * <p> 029 * For example, here is how to print out a list of the real files 030 * within the current directory: 031 * </p> 032 * <h2>Using Classic IO</h2> 033 * <pre> 034 * File dir = new File("."); 035 * String[] files = dir.list(FileFileFilter.FILE); 036 * for (String file : files) { 037 * System.out.println(file); 038 * } 039 * </pre> 040 * 041 * <h2>Using NIO</h2> 042 * <pre> 043 * final Path dir = Paths.get(""); 044 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(FileFileFilter.FILE); 045 * // 046 * // Walk one dir 047 * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor); 048 * System.out.println(visitor.getPathCounters()); 049 * System.out.println(visitor.getFileList()); 050 * // 051 * visitor.getPathCounters().reset(); 052 * // 053 * // Walk dir tree 054 * Files.<b>walkFileTree</b>(dir, visitor); 055 * System.out.println(visitor.getPathCounters()); 056 * System.out.println(visitor.getDirList()); 057 * System.out.println(visitor.getFileList()); 058 * </pre> 059 * 060 * @since 1.3 061 * @see FileFilterUtils#fileFileFilter() 062 */ 063public class FileFileFilter extends AbstractFileFilter implements Serializable { 064 065 /** 066 * Singleton instance of file filter. 067 * 068 * @since 2.9.0 069 */ 070 public static final IOFileFilter INSTANCE = new FileFileFilter(); 071 072 /** 073 * Singleton instance of file filter. 074 * 075 * @deprecated Use {@link #INSTANCE}. 076 */ 077 @Deprecated 078 public static final IOFileFilter FILE = INSTANCE; 079 080 private static final long serialVersionUID = 5345244090827540862L; 081 082 /** 083 * Restrictive constructor. 084 */ 085 protected FileFileFilter() { 086 } 087 088 /** 089 * Checks to see if the file is a file. 090 * 091 * @param file the File to check 092 * @return true if the file is a file 093 */ 094 @Override 095 public boolean accept(final File file) { 096 return file.isFile(); 097 } 098 099 /** 100 * Checks to see if the file is a file. 101 * @param file the File to check 102 * 103 * @return true if the file is a file 104 * @since 2.9.0 105 */ 106 @Override 107 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) { 108 return toFileVisitResult(Files.isRegularFile(file), file); 109 } 110 111}