Class FileSupport

java.lang.Object
org.opencastproject.util.FileSupport

public final class FileSupport extends Object
Utility class, dealing with files.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Only files will be deleted, the directory structure remains untouched.
    static final int
    Delete everything including the root directory.
  • Method Summary

    Modifier and Type
    Method
    Description
    static File
    copy(File sourceLocation, File targetLocation)
    Copies the specified file from sourceLocation to targetLocation and returns a reference to the newly created file or directory.
    static File
    copy(File sourceFile, File targetFile, boolean overwrite)
    Copies the specified sourceLocation to targetLocation and returns a reference to the newly created file or directory.
    static boolean
    Deletes the specified file and returns true if the file was deleted.
    static boolean
    delete(File f, boolean recurse)
    Deletes the specified file and returns true if the file was deleted.
    static boolean
    delete(File dir, int mode)
    Deletes the content of directory dir and, if specified, the directory itself.
    static boolean
    Delete all directories from start up to directory limit if they are empty.
    static boolean
    Like delete(File) but does not throw any IO exceptions.
    static boolean
    deleteQuietly(File f, boolean recurse)
    Like delete(File, boolean) but does not throw any IO exceptions.
    static File
    Returns the webapp's temporary work directory.
    static File
    Returns a directory subdir inside the webapp's temporary work directory.
    static boolean
    isEqual(File a, File b)
    Compare two files by their canonical paths.
    static boolean
    Check if a is a parent of b.
    static File
    link(File sourceLocation, File targetLocation)
    Links the specified file or directory from sourceLocation to targetLocation.
    static File
    link(File sourceLocation, File targetLocation, boolean overwrite)
    Links the specified file or directory from sourceLocation to targetLocation.
    static void
    Sets the webapp's temporary directory.
    static boolean
    supportsLinking(File sourceLocation, File targetLocation)
    Returns true if the operating system as well as the disk layout support creating a hard link from src to dest.

    Methods inherited from class java.lang.Object

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

    • DELETE_FILES

      public static final int DELETE_FILES
      Only files will be deleted, the directory structure remains untouched.
      See Also:
    • DELETE_ROOT

      public static final int DELETE_ROOT
      Delete everything including the root directory.
      See Also:
  • Method Details

    • copy

      public static File copy(File sourceLocation, File targetLocation) throws IOException
      Copies the specified file from sourceLocation to targetLocation and returns a reference to the newly created file or directory.

      If targetLocation is an existing directory, then the source file or directory will be copied into this directory, otherwise the source file will be copied to the file identified by targetLocation.

      Note that existing files and directories will be overwritten.

      Also note that if targetLocation is a directory than the directory itself, not only its content is copied.

      Parameters:
      sourceLocation - the source file or directory
      targetLocation - the directory to copy the source file or directory to
      Returns:
      the created copy
      Throws:
      IOException - if copying of the file or directory failed
    • copy

      public static File copy(File sourceFile, File targetFile, boolean overwrite) throws IOException
      Copies the specified sourceLocation to targetLocation and returns a reference to the newly created file or directory.

      If targetLocation is an existing directory, then the source file or directory will be copied into this directory, otherwise the source file will be copied to the file identified by targetLocation.

      If overwrite is set to false, this method throws an IOException if the target file already exists.

      Note that if targetLocation is a directory than the directory itself, not only its content is copied.

      Parameters:
      sourceFile - the source file or directory
      targetFile - the directory to copy the source file or directory to
      overwrite - true to overwrite existing files
      Returns:
      the created copy
      Throws:
      IOException - if copying of the file or directory failed
    • link

      public static File link(File sourceLocation, File targetLocation) throws IOException
      Links the specified file or directory from sourceLocation to targetLocation. If targetLocation does not exist, it will be created, if the target file already exists, an IOException will be thrown.

      If this fails (because linking is not supported on the current filesystem, then a copy is made.

      Parameters:
      sourceLocation - the source file or directory
      targetLocation - the targetLocation
      Returns:
      the created link
      Throws:
      IOException - if linking of the file or directory failed
    • link

      public static File link(File sourceLocation, File targetLocation, boolean overwrite) throws IOException
      Links the specified file or directory from sourceLocation to targetLocation. If targetLocation does not exist, it will be created.

      If this fails (because linking is not supported on the current filesystem, then a copy is made.

      If overwrite is set to false, this method throws an IOException if the target file already exists.
      Parameters:
      sourceLocation - the source file or directory
      targetLocation - the targetLocation
      overwrite - true to overwrite existing files
      Returns:
      the created link
      Throws:
      IOException - if linking of the file or directory failed
    • supportsLinking

      public static boolean supportsLinking(File sourceLocation, File targetLocation)
      Returns true if the operating system as well as the disk layout support creating a hard link from src to dest. Note that this implementation requires two files rather than directories and will overwrite any existing file that might already be present at the destination.
      Parameters:
      sourceLocation - the source file
      targetLocation - the target file
      Returns:
      true if the link was created, false otherwhise
    • deleteHierarchyIfEmpty

      public static boolean deleteHierarchyIfEmpty(File limit, File start)
      Delete all directories from start up to directory limit if they are empty. Directory limit is exclusive and will not be deleted.
      Returns:
      true if the complete hierarchy has been deleted. false in any other case.
    • isEqual

      public static boolean isEqual(File a, File b)
      Compare two files by their canonical paths.
    • isParent

      public static boolean isParent(File a, File b)
      Check if a is a parent of b. This can only be the case if a is a directory and a sub path of b. isParent(a, a) == true.
    • delete

      public static boolean delete(File f) throws IOException
      Deletes the specified file and returns true if the file was deleted.

      If f is a directory, it will only be deleted if it doesn't contain any other files or directories. To do a recursive delete, you may use delete(File, boolean).

      Parameters:
      f - the file or directory
      Throws:
      IOException
      See Also:
    • deleteQuietly

      public static boolean deleteQuietly(File f)
      Like delete(File) but does not throw any IO exceptions. In case of an IOException it will only be logged at warning level and the method returns false.
    • deleteQuietly

      public static boolean deleteQuietly(File f, boolean recurse)
      Like delete(File, boolean) but does not throw any IO exceptions. In case of an IOException it will only be logged at warning level and the method returns false.
    • delete

      public static boolean delete(File f, boolean recurse) throws IOException
      Deletes the specified file and returns true if the file was deleted.

      In the case that f references a directory, it will only be deleted if it doesn't contain other files or directories, unless recurse is set to true.

      Parameters:
      f - the file or directory
      recurse - true to do a recursive deletes for directories
      Throws:
      IOException
    • delete

      public static boolean delete(File dir, int mode)
      Deletes the content of directory dir and, if specified, the directory itself. If dir is a normal file it will always be deleted.
      Returns:
      true everthing was deleted, false otherwise
    • setTempDirectory

      public static void setTempDirectory(File tmpDir) throws IllegalArgumentException, IllegalStateException
      Sets the webapp's temporary directory. Make sure that directory exists and has write permissions turned on.
      Parameters:
      tmpDir - the new temporary directory
      Throws:
      IllegalArgumentException - if the file object doesn't represent a directory
      IllegalStateException - if the directory is write protected
    • getTempDirectory

      public static File getTempDirectory()
      Returns the webapp's temporary work directory.
      Returns:
      the temp directory
    • getTempDirectory

      public static File getTempDirectory(String subdir)
      Returns a directory subdir inside the webapp's temporary work directory.
      Parameters:
      subdir - name of the subdirectory
      Returns:
      the ready to use temp directory