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 {@link File}s that are hidden. 028 * <p> 029 * Example, showing how to print out a list of the 030 * current directory's <em>hidden</em> files: 031 * </p> 032 * <h2>Using Classic IO</h2> 033 * <pre> 034 * File dir = FileUtils.current(); 035 * String[] files = dir.list(HiddenFileFilter.HIDDEN); 036 * for (String file : files) { 037 * System.out.println(file); 038 * } 039 * </pre> 040 * 041 * <p> 042 * Example, showing how to print out a list of the 043 * current directory's <em>visible</em> (not hidden) files: 044 * </p> 045 * 046 * <pre> 047 * File dir = FileUtils.current(); 048 * String[] files = dir.list(HiddenFileFilter.VISIBLE); 049 * for (String file : files) { 050 * System.out.println(file); 051 * } 052 * </pre> 053 * 054 * <h2>Using NIO</h2> 055 * <pre> 056 * final Path dir = PathUtils.current(); 057 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(HiddenFileFilter.HIDDEN); 058 * // 059 * // Walk one directory 060 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor); 061 * System.out.println(visitor.getPathCounters()); 062 * System.out.println(visitor.getFileList()); 063 * // 064 * visitor.getPathCounters().reset(); 065 * // 066 * // Walk directory tree 067 * Files.<strong>walkFileTree</strong>(dir, visitor); 068 * System.out.println(visitor.getPathCounters()); 069 * System.out.println(visitor.getDirList()); 070 * System.out.println(visitor.getFileList()); 071 * </pre> 072 * <h2>Deprecating Serialization</h2> 073 * <p> 074 * <em>Serialization is deprecated and will be removed in 3.0.</em> 075 * </p> 076 * 077 * @since 1.3 078 */ 079public class HiddenFileFilter extends AbstractFileFilter implements Serializable { 080 081 /** Singleton instance of <em>hidden</em> filter */ 082 public static final IOFileFilter HIDDEN = new HiddenFileFilter(); 083 084 private static final long serialVersionUID = 8930842316112759062L; 085 086 /** Singleton instance of <em>visible</em> filter */ 087 public static final IOFileFilter VISIBLE = HIDDEN.negate(); 088 089 /** 090 * Restrictive constructor. 091 */ 092 protected HiddenFileFilter() { 093 } 094 095 /** 096 * Checks to see if the file is hidden. 097 * 098 * @param file the File to check 099 * @return {@code true} if the file is 100 * <em>hidden</em>, otherwise {@code false}. 101 */ 102 @Override 103 public boolean accept(final File file) { 104 return file == null || file.isHidden(); 105 } 106 107 /** 108 * Checks to see if the file is hidden. 109 * 110 * @param file the File to check 111 * @param attributes the path's basic attributes (may be null). 112 * @return {@code true} if the file is <em>hidden</em>, otherwise {@code false}. 113 * @since 2.9.0 114 */ 115 @Override 116 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) { 117 return get(() -> toFileVisitResult(file == null || Files.isHidden(file))); 118 } 119 120}