1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.io.filefilter; 18 19 import java.io.File; 20 import java.io.Serializable; 21 import java.nio.file.FileVisitResult; 22 import java.nio.file.Files; 23 import java.nio.file.Path; 24 import java.nio.file.attribute.BasicFileAttributes; 25 26 /** 27 * This filter accepts {@link File}s that are files (not directories). 28 * <p> 29 * For example, here is how to print out a list of the real files 30 * within the current directory: 31 * </p> 32 * <h2>Using Classic IO</h2> 33 * <pre> 34 * File dir = FileUtils.current(); 35 * String[] files = dir.list(FileFileFilter.INSTANCE); 36 * for (String file : files) { 37 * System.out.println(file); 38 * } 39 * </pre> 40 * 41 * <h2>Using NIO</h2> 42 * <pre> 43 * final Path dir = PathUtils.current(); 44 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(FileFileFilter.INSTANCE); 45 * // 46 * // Walk one dir 47 * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor); 48 * System.out.println(visitor.getPathCounters()); 49 * System.out.println(visitor.getFileList()); 50 * // 51 * visitor.getPathCounters().reset(); 52 * // 53 * // Walk dir tree 54 * Files.<b>walkFileTree</b>(dir, visitor); 55 * System.out.println(visitor.getPathCounters()); 56 * System.out.println(visitor.getDirList()); 57 * System.out.println(visitor.getFileList()); 58 * </pre> 59 * <h2>Deprecating Serialization</h2> 60 * <p> 61 * <em>Serialization is deprecated and will be removed in 3.0.</em> 62 * </p> 63 * 64 * @since 1.3 65 * @see FileFilterUtils#fileFileFilter() 66 */ 67 public class FileFileFilter extends AbstractFileFilter implements Serializable { 68 69 /** 70 * Singleton instance of file filter. 71 * 72 * @since 2.9.0 73 */ 74 public static final IOFileFilter INSTANCE = new FileFileFilter(); 75 76 /** 77 * Singleton instance of file filter. 78 * 79 * @deprecated Use {@link #INSTANCE}. 80 */ 81 @Deprecated 82 public static final IOFileFilter FILE = INSTANCE; 83 84 private static final long serialVersionUID = 5345244090827540862L; 85 86 /** 87 * Restrictive constructor. 88 */ 89 protected FileFileFilter() { 90 } 91 92 /** 93 * Checks to see if the file is a file. 94 * 95 * @param file the File to check 96 * @return true if the file is a file 97 */ 98 @Override 99 public boolean accept(final File file) { 100 return file != null && file.isFile(); 101 } 102 103 /** 104 * Checks to see if the file is a file. 105 * @param file the File to check 106 * 107 * @return true if the file is a file 108 * @since 2.9.0 109 */ 110 @Override 111 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) { 112 return toFileVisitResult(file != null && Files.isRegularFile(file)); 113 } 114 115 }