View Javadoc
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 can be written to.
28   * <p>
29   * Example, showing how to print out a list of the current directory's <i>writable</i> files:
30   * </p>
31   * <h2>Using Classic IO</h2>
32   * <pre>
33   * File dir = FileUtils.current();
34   * String[] files = dir.list(CanWriteFileFilter.CAN_WRITE);
35   * for (String file : files) {
36   *     System.out.println(file);
37   * }
38   * </pre>
39   * <p>
40   * Example, showing how to print out a list of the current directory's <i>un-writable</i> files:
41   * </p>
42   * <pre>
43   * File dir = FileUtils.current();
44   * String[] files = dir.list(CanWriteFileFilter.CANNOT_WRITE);
45   * for (String file : files) {
46   *     System.out.println(file);
47   * }
48   * </pre>
49   * <p>
50   * <b>N.B.</b> For read-only files, use {@code CanReadFileFilter.READ_ONLY}.
51   * </p>
52   * <h2>Deprecating Serialization</h2>
53   * <p>
54   * <em>Serialization is deprecated and will be removed in 3.0.</em>
55   * </p>
56   *
57   * @since 1.3
58   */
59  public class CanWriteFileFilter extends AbstractFileFilter implements Serializable {
60  
61      /** Singleton instance of <i>writable</i> filter */
62      public static final IOFileFilter CAN_WRITE = new CanWriteFileFilter();
63  
64      /** Singleton instance of not <i>writable</i> filter */
65      public static final IOFileFilter CANNOT_WRITE = CAN_WRITE.negate();
66  
67      private static final long serialVersionUID = 5132005214688990379L;
68  
69      /**
70       * Restrictive constructor.
71       */
72      protected CanWriteFileFilter() {
73      }
74  
75      /**
76       * Checks to see if the file can be written to.
77       *
78       * @param file the File to check
79       * @return {@code true} if the file can be written to, otherwise {@code false}.
80       */
81      @Override
82      public boolean accept(final File file) {
83          return file != null && file.canWrite();
84      }
85  
86      /**
87       * Checks to see if the file can be written to.
88       * @param file the File to check
89       *
90       * @return {@code true} if the file can be written to, otherwise {@code false}.
91       * @since 2.9.0
92       */
93      @Override
94      public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
95          return toFileVisitResult(file != null && Files.isWritable(file));
96      }
97  
98  }