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 can be read. 028 * <p> 029 * Example, showing how to print out a list of the current directory's <i>readable</i> files: 030 * </p> 031 * <h2>Using Classic IO</h2> 032 * <pre> 033 * File dir = new File("."); 034 * String[] files = dir.list(CanReadFileFilter.CAN_READ); 035 * for (String file : files) { 036 * System.out.println(file); 037 * } 038 * </pre> 039 * 040 * <p> 041 * Example, showing how to print out a list of the current directory's <i>un-readable</i> files: 042 * 043 * <pre> 044 * File dir = new File("."); 045 * String[] files = dir.list(CanReadFileFilter.CANNOT_READ); 046 * for (String file : files) { 047 * System.out.println(file); 048 * } 049 * </pre> 050 * 051 * <p> 052 * Example, showing how to print out a list of the current directory's <i>read-only</i> files: 053 * 054 * <pre> 055 * File dir = new File("."); 056 * String[] files = dir.list(CanReadFileFilter.READ_ONLY); 057 * for (String file : files) { 058 * System.out.println(file); 059 * } 060 * </pre> 061 * 062 * @since 1.3 063 */ 064public class CanReadFileFilter extends AbstractFileFilter implements Serializable { 065 066 /** Singleton instance of <i>readable</i> filter */ 067 public static final IOFileFilter CAN_READ = new CanReadFileFilter(); 068 069 /** Singleton instance of not <i>readable</i> filter */ 070 public static final IOFileFilter CANNOT_READ = CAN_READ.negate(); 071 072 /** Singleton instance of <i>read-only</i> filter */ 073 public static final IOFileFilter READ_ONLY = CAN_READ.and(CanWriteFileFilter.CANNOT_WRITE); 074 075 private static final long serialVersionUID = 3179904805251622989L; 076 077 /** 078 * Restrictive constructor. 079 */ 080 protected CanReadFileFilter() { 081 } 082 083 /** 084 * Checks to see if the file can be read. 085 * 086 * @param file the File to check. 087 * @return {@code true} if the file can be read, otherwise {@code false}. 088 */ 089 @Override 090 public boolean accept(final File file) { 091 return file.canRead(); 092 } 093 094 /** 095 * Checks to see if the file can be read. 096 * @param file the File to check. 097 * 098 * @return {@code true} if the file can be read, otherwise {@code false}. 099 * @since 2.9.0 100 */ 101 @Override 102 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) { 103 return toFileVisitResult(Files.isReadable(file), file); 104 } 105 106}