Class FileHelper

java.lang.Object
com.tangosol.io.FileHelper

public class FileHelper extends Object
A Collection of helper methods for files.
Author:
jh 2012.07.13
  • Constructor Details

    • FileHelper

      public FileHelper()
  • Method Details

    • toFilename

      public static String toFilename(String sName)
      Given a string, return a derivative version that is safe to use for a filename.
      Parameters:
      sName - the string
      Returns:
      a derivative of the given string that is safe to use as a filename (may be the same as the string supplied)
    • getPath

      public static String getPath(File file)
      Return the path of a file.

      The implementation attempts to get the canonical path and iff this call abruptly fails is the absolute path returned.

      Parameters:
      file - the File to be interrogated to return the path
      Returns:
      the path of the provided file
    • ensureDir

      public static File ensureDir(File file) throws IOException
      Validate that the given File represents a directory, creating it if it doesn't already exist.
      Parameters:
      file - the File to check
      Returns:
      the validated File
      Throws:
      IOException - on error creating the directory
    • validateDir

      public static void validateDir(File file) throws IOException
      Validate that the given File exists and represents a directory.
      Parameters:
      file - the File to check
      Throws:
      IOException - if the given File doesn't exist or doesn't represent a directory
    • copyDir

      public static void copyDir(File fileDirFrom, File fileDirTo) throws IOException
      Create a deep copy of the specified directory into the specified target directory.
      Parameters:
      fileDirFrom - the directory to copy from
      fileDirTo - the directory to copy into
      Throws:
      IOException - if an error occurred copying the directory
    • deleteDir

      public static void deleteDir(File fileDir) throws IOException
      Delete a directory recursively.
      Parameters:
      fileDir - directory to delete
      Throws:
      IOException - if an error occurred deleting the directory
    • deleteDirSilent

      public static void deleteDirSilent(File fileDir)
      Recursively delete a directory suppressing any raised exceptions.
      Parameters:
      fileDir - directory to delete
    • moveDir

      public static void moveDir(File fileDirFrom, File fileDirTo) throws IOException
      Move the specified directory to the specified destination path.
      Parameters:
      fileDirFrom - the directory to move
      fileDirTo - the destination path
      Throws:
      IOException - if an error occurred moving the directory
    • createTempDir

      public static File createTempDir() throws IOException
      Create a unique temporary directory.
      Returns:
      a unique temporary directory
      Throws:
      IOException
    • sizeDir

      public static long sizeDir(File fileDir)
      Return the approximate disk usage in bytes for a given directory. Note the size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons.
      Parameters:
      fileDir - the directory to size
      Returns:
      the number of total bytes used by the files in the directory
    • copyFile

      public static void copyFile(File fileFrom, File fileTo) throws IOException
      Copy the specified file according to the source and destination File objects preserving the time-stamp of the source.
      Parameters:
      fileFrom - the File to copy from
      fileTo - the File to copy to
      Throws:
      IOException - if an error occurred copying the file
    • moveFile

      public static void moveFile(File fileFrom, File fileTo) throws IOException
      Move the specified file to the specified destination path.
      Parameters:
      fileFrom - the file to move
      fileTo - the destination path
      Throws:
      IOException - if an error occurred moving the file
    • lockFile

      public static FileLock lockFile(File file)
      Obtain an exclusive lock on the specified file. The lock should be released with the unlockFile method.
      Parameters:
      file - the file to lock
      Returns:
      a FileLock if an exclusive lock was obtained, null otherwise
    • unlockFile

      public static void unlockFile(FileLock lock)
      Release an exclusive lock on a file.
      Parameters:
      lock - the exclusive lock to release
    • separatorsToUnix

      public static String separatorsToUnix(String path)
      Converts all separators to the Unix separator of forward slash.
      Parameters:
      path - the path to be changed, null ignored
      Returns:
      the updated path
    • getPrefixLength

      public static int getPrefixLength(String filename)
      Returns the length of the filename prefix, such as C:/ or ~/.

      This method will handle a file in either Unix or Windows format.

      The prefix length includes the first slash in the full filename if applicable. Thus, it is possible that the length returned is greater than the length of the input string.

       Windows:
       a\b\c.txt           --> ""          --> relative
       \a\b\c.txt          --> "\"         --> current drive absolute
       C:a\b\c.txt         --> "C:"        --> drive relative
       C:\a\b\c.txt        --> "C:\"       --> absolute
       \\server\a\b\c.txt  --> "\\server\" --> UNC
       \\\a\b\c.txt        -->  error, length = -1
      
       Unix:
       a/b/c.txt           --> ""          --> relative
       /a/b/c.txt          --> "/"         --> absolute
       ~/a/b/c.txt         --> "~/"        --> current user
       ~                   --> "~/"        --> current user (slash added)
       ~user/a/b/c.txt     --> "~user/"    --> named user
       ~user               --> "~user/"    --> named user (slash added)
       //server/a/b/c.txt  --> "//server/"
       ///a/b/c.txt        --> error, length = -1
       

      The output will be the same irrespective of the machine that the code is running on. ie. both Unix and Windows prefixes are matched regardless.

      Note that a leading // (or \\) is used to indicate a UNC name on Windows. These must be followed by a server name, so double-slashes are not collapsed to a single slash at the start of the filename.

      Parameters:
      filename - the filename to find the prefix in, null returns -1
      Returns:
      the length of the prefix, -1 if invalid or null
    • indexOfLastSeparator

      public static int indexOfLastSeparator(String filename)
      Returns the index of the last directory separator character.

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

      The output will be the same irrespective of the machine that the code is running on.

      Parameters:
      filename - the filename to find the last path separator in, null returns -1
      Returns:
      the index of the last separator character, or -1 if there is no such character
    • indexOfExtension

      public static int indexOfExtension(String filename)
      Returns the index of the last extension separator character, which is a dot.

      This method also checks that there is no directory separator after the last dot. To do this it uses indexOfLastSeparator(String) which will handle a file in either Unix or Windows format.

      The output will be the same irrespective of the machine that the code is running on.

      Parameters:
      filename - the filename to find the last extension separator in, null returns -1
      Returns:
      the index of the last extension separator character, or -1 if there is no such character
    • getPathNoEndSeparator

      public static String getPathNoEndSeparator(String filename)
      Gets the path from a full filename, which excludes the prefix, and also excluding the final directory separator.

      This method will handle a file in either Unix or Windows format. The method is entirely text based, and returns the text before the last forward or backslash.

       C:\a\b\c.txt --> a\b
       ~/a/b/c.txt  --> a/b
       a.txt        --> ""
       a/b/c        --> a/b
       a/b/c/       --> a/b/c
       

      The output will be the same irrespective of the machine that the code is running on.

      Parameters:
      filename - the filename to query, null returns null
      Returns:
      the path of the file, an empty string if none exists, null if invalid. Null bytes inside string will be removed
    • 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/    --> ""
       

      The output will be the same irrespective of the machine that the code is running on.

      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. Null bytes inside string will be removed
    • 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.txt     --> a
       a/b/c     --> c
       a/b/c/    --> ""
       

      The output will be the same irrespective of the machine that the code is running on.

      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. Null bytes inside string will be removed
    • 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.

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

      The output will be the same irrespective of the machine that the code is running on.

      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
       

      The output will be the same irrespective of the machine that the code is running on.

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