View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.utils;
20  
21  import java.io.File;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import java.nio.file.Files;
26  import java.util.Arrays;
27  import java.util.Collections;
28  
29  /**
30   * A set of utilities to help process files.
31   */
32  public final class FileUtils {
33      private FileUtils() {
34          // do not instantiate
35      }
36  
37      /**
38       * Creates a directory if it does not exist.
39       * @param dir the directory to make.
40       */
41      public static void mkDir(final File dir) {
42          boolean ignored = dir.mkdirs();
43      }
44  
45      /**
46       * Deletes a file if it exists (ignoring all IO-related exceptions).
47       * @param file the file to delete.
48       */
49      public static void delete(final File file) {
50          if (file.exists()) {
51              try {
52                  if (file.isDirectory()) {
53                      org.apache.commons.io.FileUtils.deleteDirectory(file);
54                  } else {
55                      Files.delete(file.toPath());
56                  }
57              } catch (IOException ignore) {
58                  // ignore
59              }
60          }
61      }
62  
63      /**
64       * Writes a test file.
65       * @param dir The directory to write the file in.
66       * @param name the name of the file.
67       * @param lines the lines to write into the file.
68       * @return the new File.
69       */
70      public static File writeFile(final File dir, final String name, final Iterable<String> lines) {
71          if (dir == null) {
72              throw new IllegalArgumentException("base directory not specified");
73          }
74          mkDir(dir);
75          File file = new File(dir, name);
76          try (PrintWriter writer = new PrintWriter(new FileWriter(file))) {
77              lines.forEach(writer::println);
78          } catch (IOException e) {
79              throw new RuntimeException(e.getMessage(), e);
80          }
81          return file;
82      }
83  
84      /**
85       * Writes a test file.
86       * @param dir The directory to write the file in.
87       * @param name the name of the file.
88       * @param lines the lines to write into the file.
89       * @return the new File.
90       */
91      public static File writeFile(final File dir, final String name, final String... lines) {
92          return writeFile(dir, name, Arrays.asList(lines));
93      }
94  
95      /**
96       * Writes a text file containing the name of the file.
97       * @param dir The directory to write the file into.
98       * @param name the name of the file.
99       * @return the new file.
100      */
101     public static File writeFile(final File dir, final String name) {
102         return writeFile(dir, name, Collections.singletonList(name));
103     }
104 }