Class FileUtils

java.lang.Object
com.axelor.common.FileUtils

public final class FileUtils extends Object
This class provides some helper methods to deal with files.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    copyDirectory(File source, File target)
    Copy the source directory to the target directory.
    static void
    copyDirectory(Path source, Path target)
    Copy the source directory to the target directory.
    static void
    copyFile(File source, File destination)
    Copy the contents of the given source file to the given destination file.
    static void
    copyPath(Path source, Path destination)
     
    static void
    deleteDirectory(File directory)
    Delete the given directory recursively.
    static void
    deleteDirectory(Path directory)
    Delete the given directory recursively.
    static String
    getExtension(String fileName)
    Gets the extension part of the given fileName
    static File
    getFile(File directory, String next, String... more)
    Get a file from the given path elements.
    static File
    getFile(String first, String... more)
    Get a file from the given path elements.
    static String
    getFileName(String filePath)
    Return the name of the file
    static String
    getFileName(Path filePath)
    Return the name of the file
    static boolean
    isChildPath(Path parent, Path candidate)
    Check if the candidate path is located inside the parent path
    static String
    safeFileName(String originalFileName)
    Sanitizes a filename, replacing with dash Removes special characters that are illegal in filenames on certain operating systems Replaces spaces and consecutive underscore with a single dash Trims dot, dash and underscore from beginning and end of filename (with and without the extension part)
    static String
    Strip the extension part of the given fileName
    static void
    write(File file, InputStream inputStream)
    Copy the contents of the given InputStream to the given File.
    static void
    write(File file, InputStream inputStream, boolean append)
    Copy the contents of the given InputStream to the given File.
    static void
    write(Path path, InputStream inputStream)
    Copy the contents of the given InputStream to the given Path.
    static void
    write(Path path, InputStream inputStream, boolean append)
    Copy the contents of the given InputStream to the given Path.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • getFile

      public static File getFile(String first, String... more)
      Get a file from the given path elements.
      Parameters:
      first - the first path element
      more - the additional path elements
      Returns:
      the file
    • getFile

      public static File getFile(File directory, String next, String... more)
      Get a file from the given path elements.
      Parameters:
      directory - the parent directory
      next - next path element
      more - additional path elements
      Returns:
      the file
    • copyDirectory

      public static void copyDirectory(File source, File target) throws IOException
      Copy the source directory to the target directory.
      Parameters:
      source - the source directory
      target - the target directory
      Throws:
      IOException - if IO error occurs during copying
    • copyDirectory

      public static void copyDirectory(Path source, Path target) throws IOException
      Copy the source directory to the target directory.
      Parameters:
      source - the source directory
      target - the target directory
      Throws:
      IOException - if IO error occurs during copying
    • deleteDirectory

      public static void deleteDirectory(File directory) throws IOException
      Delete the given directory recursively.
      Parameters:
      directory - the directory to delete
      Throws:
      IOException - in case deletion is unsuccessful
    • deleteDirectory

      public static void deleteDirectory(Path directory) throws IOException
      Delete the given directory recursively.
      Parameters:
      directory - the directory to delete
      Throws:
      IOException - in case deletion is unsuccessful
    • safeFileName

      public static String safeFileName(String originalFileName)
      Sanitizes a filename, replacing with dash
      • Removes special characters that are illegal in filenames on certain operating systems
      • Replaces spaces and consecutive underscore with a single dash
      • Trims dot, dash and underscore from beginning and end of filename (with and without the extension part)
      Parameters:
      originalFileName - The filename to be sanitized
      Returns:
      string The sanitized filename
    • getExtension

      public static String getExtension(String fileName)
      Gets the extension part of the given fileName

      It returns the extension of the fileName after the last dot

      Parameters:
      fileName - the fileName to retrieve the extension of
      Returns:
      the extension of the file or an empty string is not found or null if the given filename is null
    • stripExtension

      public static String stripExtension(String fileName)
      Strip the extension part of the given fileName

      It returns the name of file before the last dot

      Parameters:
      fileName - the fileName to strip the extension
      Returns:
      the file name
    • getFileName

      public static String getFileName(String filePath)
      Return the name of the file
      Parameters:
      filePath - the file path
      Returns:
      name of the file
    • getFileName

      public static String getFileName(Path filePath)
      Return the name of the file
      Parameters:
      filePath - the file path
      Returns:
      name of the file
    • copyFile

      public static void copyFile(File source, File destination) throws IOException
      Copy the contents of the given source file to the given destination file.
      Parameters:
      source - the source file to copy from
      destination - the destination file to copy to
      Throws:
      IOException - in case of I/O errors
    • copyPath

      public static void copyPath(Path source, Path destination) throws IOException
      Throws:
      IOException
    • write

      public static void write(File file, InputStream inputStream) throws IOException
      Copy the contents of the given InputStream to the given File.
      Parameters:
      file - the file to copy to
      inputStream - the inputStream to copy from
      Throws:
      IOException - in case of I/O errors
    • write

      public static void write(File file, InputStream inputStream, boolean append) throws IOException
      Copy the contents of the given InputStream to the given File.
      Parameters:
      file - the file to copy to
      inputStream - the inputStream to copy from
      append - write to the end of the file rather than the beginning
      Throws:
      IOException - in case of I/O errors
    • write

      public static void write(Path path, InputStream inputStream) throws IOException
      Copy the contents of the given InputStream to the given Path.
      Parameters:
      path - the path to copy to
      inputStream - the inputStream to copy from
      Throws:
      IOException - in case of I/O errors
    • write

      public static void write(Path path, InputStream inputStream, boolean append) throws IOException
      Copy the contents of the given InputStream to the given Path.
      Parameters:
      path - the path to copy to
      inputStream - the inputStream to copy from
      append - write to the end of the file rather than the beginning
      Throws:
      IOException - in case of I/O errors
    • isChildPath

      public static boolean isChildPath(Path parent, Path candidate)
      Check if the candidate path is located inside the parent path
      Parameters:
      parent - the parent path
      candidate - the candidate path
      Returns:
      tru if the candidate path is located inside the parent path, otherwise false