Class FileUtils

java.lang.Object
org.openl.util.FileUtils

public class FileUtils extends Object
A set of methods to work with a file system.
Author:
Yury Molchan
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • getTempDirectoryPath

      public static String getTempDirectoryPath()
      Returns the path to the system temporary directory.
      Returns:
      the path to the system temporary directory.
    • copy

      public static void copy(File src, File dest) throws IOException
      Copies a file to a new location preserving the file date.

      This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

      Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

      Parameters:
      src - an existing file to copy, must not be null
      dest - the new file, must not be null
      Throws:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
    • move

      public static void move(File src, File dest) throws IOException
      Moves a directory or a file.

      When the destination directory or file is on another file system, do a "copy and delete".

      Parameters:
      src - the directory or the file to be moved
      dest - the destination directory or file
      Throws:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs moving the file
    • delete

      public static void delete(File file) throws IOException
      Deletes a file. If file is a directory, delete it and all sub-directories.

      The difference between File.delete() and this method are:

      • A directory to be deleted does not have to be empty.
      • You get exceptions when a file or directory cannot be deleted.
      Parameters:
      file - file or directory to delete, must not be null
      Throws:
      NullPointerException - if the directory is null
      FileNotFoundException - if the file has not been found
      IOException - in case deletion is unsuccessful
    • delete

      public static void delete(Path root) throws IOException
      Deletes a path. If provided path is a directory, delete it and all sub-directories.
      Parameters:
      root - path to file or directory to delete, must not be null
      Throws:
      NullPointerException - if the directory is null
      FileNotFoundException - if the file has not been found
      IOException - in case deletion is unsuccessful
    • deleteQuietly

      public static void deleteQuietly(File file)
      Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.

      The difference between File.delete() and this method are:

      • A directory to be deleted does not have to be empty.
      • No exceptions are thrown when a file or directory cannot be deleted.
      Parameters:
      file - file or directory to delete, can be null
    • deleteQuietly

      public static void deleteQuietly(Path path)
    • getName

      public static String getName(String filename)
      Gets the name minus the path from a full filename.

      This method will handle a file in either Unix or Windows format. The text after the last forward or backslash is returned.

       a/b/c.txt --> c.txt
       a.txt     --> a.txt
       a/b/c     --> c
       a/b/c/    --> ""
       

      Parameters:
      filename - the filename to query, null returns null
      Returns:
      the name of the file without the path, or an empty string if none exists
    • getBaseName

      public static String getBaseName(String filename)
      Gets the base name, minus the full path and extension, from a full filename.

      This method will handle a file in either Unix or Windows format. The text after the last forward or backslash and before the last dot is returned.

       a/b/c.txt --> c
       a.b.txt   --> a.b
       a/b/c     --> c
       a/b/c/    --> ""
       

      Parameters:
      filename - the filename to query, null returns null
      Returns:
      the name of the file without the path, or an empty string if none exists
    • getExtension

      public static String getExtension(String filename)
      Gets the extension of a filename.

      This method returns the textual part of the filename after the last dot. There must be no directory separator after the dot.

       a/b/c.txt    --> txt
       a.b.txt      --> txt
       a/b.txt/c    --> ""
       a/b/c        --> ""
       

      Parameters:
      filename - the filename to retrieve the extension of.
      Returns:
      the extension of the file or an empty string if none exists or null if the filename is null.
    • removeExtension

      public static String removeExtension(String filename)
      Removes the extension from a filename.

      This method returns the textual part of the filename before the last dot. There must be no directory separator after the dot.

       foo.txt    --> foo
       a\b\c.jpg  --> a\b\c
       a\b\c      --> a\b\c
       a.b\c      --> a.b\c
       

      Parameters:
      filename - the filename to query, null returns null
      Returns:
      the filename minus the extension