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