Package nl.saxion.app

Class FileUtils

java.lang.Object
nl.saxion.app.FileUtils

public class FileUtils extends Object
File operations utility class. This class gives you access to the file system and lets you list directories, copy, move and delete files/directories.
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • list

      public ArrayList<String> list()
      Get all files and directories in the current working directory This method does not work recursively.
      Returns:
      List with filenames
    • list

      public ArrayList<String> list(String path)
      Get all files and directories in a certain directory. This method does not work recursively.
      Parameters:
      path - path on the file system
      Returns:
      List with filenames
    • listFiles

      public ArrayList<String> listFiles(String path)
      Get all files in a certain directory. This method does not work recursively.
      Parameters:
      path - path on the file system
      Returns:
      List with filenames
    • listDirs

      public ArrayList<String> listDirs(String path)
      Get all subdirectories in a certain directory. This method does not work recursively.
      Parameters:
      path - path on the file system
      Returns:
      List with filenames
    • exists

      public boolean exists(String path)
      Check if a certain file or directory does exist.
      Parameters:
      path - path on the file system
      Returns:
      true, if the file/directory exists.
    • isFile

      public boolean isFile(String path)
      Check if a path is of type file.
      Parameters:
      path - path on the file system
      Returns:
      true, if path is file.
    • isDirectory

      public boolean isDirectory(String path)
      Check if a path is of type directory.
      Parameters:
      path - path on the file system
      Returns:
      true, if path is directory.
    • getFileInfo

      public FileUtils.FileInfo getFileInfo(String path)
      Get information about a file or a directory.
      Parameters:
      path - path on the file system
      Returns:
      FileInfo object with information about the file
    • readFileToString

      public String readFileToString(String path)
      Read contents of a file to a String.
      Parameters:
      path - path on the file system
      Returns:
      Contents of the file.
    • getWorkingDirectory

      public String getWorkingDirectory()
      Returns the current working directory, as full (absolute) path.
      Returns:
      current working directory
    • getParentDirectory

      public String getParentDirectory(String path)
      Get the parent directory of a certain path. Useful when traversing the filesystem.
      Parameters:
      path - path on the filesystem
      Returns:
      parent directory
    • toRelativePath

      public String toRelativePath(String absolutePath)
      Converts an absolute path to a relative path.
      Parameters:
      absolutePath - Absolute path
      Returns:
      relative path
    • toAbsolutePath

      public String toAbsolutePath(String relativePath)
      Converts an relative path to an absolute path.
      Parameters:
      relativePath - Relative path
      Returns:
      absolute path
    • copy

      public void copy(String fromPath, String toPath)
      Copy file or directory to new location.
      Parameters:
      fromPath - from
      toPath - to
    • move

      public void move(String fromPath, String toPath)
      Move file or directory to new location.
      Parameters:
      fromPath - from
      toPath - to
    • delete

      public void delete(String path)
      Delete a single file or an empty directory on the filesystem. This method does not work recursively.
      Parameters:
      path - path on the file system.
    • deleteDirectoryAndContents

      public void deleteDirectoryAndContents(String path)
      WARNING: Deletes all directories with all its files and subdirectories. Be careful with this operation!!!
      Parameters:
      path - path on the file system.
    • createFile

      public void createFile(String path)
      Create an empty file on the file system.
      Parameters:
      path - path on the filesystem
    • writeToFile

      public void writeToFile(String path, String contents)
      Overwrites an existing file and replaces the contents.
      Parameters:
      path - path on the file system
      contents - contents to write to the file
    • appendToFile

      public void appendToFile(String path, String contents)
      Append contents to an existing file.
      Parameters:
      path - path on the file system
      contents - contents to write to the file
    • createDirectory

      public void createDirectory(String path)
      Creates a single directory. This operation will NOT work recursively and only create a single directory. In case you are looking for recursive directory creation, see @createDirectories(String path)
      Parameters:
      path - path on the file system
    • createDirectories

      public void createDirectories(String path)
      Creates all directories in a path recursively.
      Parameters:
      path - path on the file system