/**
   * Finds all files in folder and in it's sub-tree of specified depth.
   *
   * @param file Starting folder
   * @param maxDepth Depth of the tree. If 1 - just look in the folder, no sub-folders.
   * @param filter file filter.
   * @return List of found files.
   */
  public static List<VisorLogFile> fileTree(File file, int maxDepth, @Nullable FileFilter filter) {
    if (file.isDirectory()) {
      File[] files = (filter == null) ? file.listFiles() : file.listFiles(filter);

      if (files == null) return Collections.emptyList();

      List<VisorLogFile> res = new ArrayList<>(files.length);

      for (File f : files) {
        if (f.isFile() && f.length() > 0) res.add(new VisorLogFile(f));
        else if (maxDepth > 1) res.addAll(fileTree(f, maxDepth - 1, filter));
      }

      return res;
    }

    return F.asList(new VisorLogFile(file));
  }
Example #2
0
 /**
  * Searches for the directory 'dirPath'; if not found, tries to create it, then returns a File
  * object for that directory.
  */
 public static File ensureDirExists(final String dirPath) {
   File dirpath = new File(dirPath);
   if (!dirpath.isDirectory()) {
     if (!dirpath.exists()) {
       printDebug("[Meta] " + dirpath + " does not exist: creating it...");
       try {
         Files.createDirectories(Paths.get(dirpath.getPath()));
         return dirpath;
       } catch (IOException e) {
         printDebug("[Meta] Exception while creating directory:");
         e.printStackTrace();
         return null;
       }
     } else {
       printDebug(
           "[Meta] Error: path `"
               + dirpath
               + "' is not a valid directory path, and could not create it.");
       return null;
     }
   } else return dirpath;
 }
  /*
   * Deletes the directory at the location passed. If deleteIfFull is true
   * then the method gets all the files in the directory and tries to delete
   * them. If deleteIfFull is false the method will attempt to delete the
   * directory. If the directory contains files, the method will return false.
   * Returns true if and only if the directory was deleted;
   * returns false otherwise
   */
  public static boolean deleteDirectory(File dir, boolean deleteIfFull) {
    // Checks if the file exists and if it is actually a directory
    if (dir.exists() && dir.isDirectory()) {
      // Checks if deleteIfFull is true
      if (deleteIfFull) {
        // Goes through each file in the directory and attempts to delete them
        File[] files = dir.listFiles();
        if (files != null) {
          for (File f : files) {
            f.delete();
          }
        }
      }
      // If the directory was deleted successfully then return true
      if (dir.delete()) {
        return true;
      }
    }

    // Return false otherwise
    return false;
  }