Class FileHelper
- Author:
- jh 2012.07.13
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Create a deep copy of the specified directory into the specified target directory.static void
Copy the specified file according to the source and destination File objects preserving the time-stamp of the source.static File
Create a unique temporary directory.static void
Delete a directory recursively.static void
deleteDirSilent
(File fileDir) Recursively delete a directory suppressing any raised exceptions.static File
Validate that the given File represents a directory, creating it if it doesn't already exist.static String
getBaseName
(String filename) Gets the base name, minus the full path and extension, from a full filename.static String
getExtension
(String filename) Gets the extension of a filename.static String
Gets the name minus the path from a full filename.static String
Return the path of a file.static String
getPathNoEndSeparator
(String filename) Gets the path from a full filename, which excludes the prefix, and also excluding the final directory separator.static int
getPrefixLength
(String filename) Returns the length of the filename prefix, such asC:/
or~/
.static int
indexOfExtension
(String filename) Returns the index of the last extension separator character, which is a dot.static int
indexOfLastSeparator
(String filename) Returns the index of the last directory separator character.static FileLock
Obtain an exclusive lock on the specified file.static void
Move the specified directory to the specified destination path.static void
Move the specified file to the specified destination path.static String
removeExtension
(String filename) Removes the extension from a filename.static String
separatorsToUnix
(String path) Converts all separators to the Unix separator of forward slash.static long
Return the approximate disk usage in bytes for a given directory.static String
toFilename
(String sName) Given a string, return a derivative version that is safe to use for a filename.static void
unlockFile
(FileLock lock) Release anexclusive lock
on a file.static void
validateDir
(File file) Validate that the given File exists and represents a directory.
-
Constructor Details
-
FileHelper
public FileHelper()
-
-
Method Details
-
toFilename
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
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
- theFile
to be interrogated to return the path- Returns:
- the path of the provided file
-
ensureDir
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
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
Create a deep copy of the specified directory into the specified target directory.- Parameters:
fileDirFrom
- the directory to copy fromfileDirTo
- the directory to copy into- Throws:
IOException
- if an error occurred copying the directory
-
deleteDir
Delete a directory recursively.- Parameters:
fileDir
- directory to delete- Throws:
IOException
- if an error occurred deleting the directory
-
deleteDirSilent
Recursively delete a directory suppressing any raised exceptions.- Parameters:
fileDir
- directory to delete
-
moveDir
Move the specified directory to the specified destination path.- Parameters:
fileDirFrom
- the directory to movefileDirTo
- the destination path- Throws:
IOException
- if an error occurred moving the directory
-
createTempDir
Create a unique temporary directory.- Returns:
- a unique temporary directory
- Throws:
IOException
-
sizeDir
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
Copy the specified file according to the source and destination File objects preserving the time-stamp of the source.- Parameters:
fileFrom
- theFile
to copy fromfileTo
- theFile
to copy to- Throws:
IOException
- if an error occurred copying the file
-
moveFile
Move the specified file to the specified destination path.- Parameters:
fileFrom
- the file to movefileTo
- the destination path- Throws:
IOException
- if an error occurred moving the file
-
lockFile
Obtain an exclusive lock on the specified file. The lock should be released with theunlockFile
method.- Parameters:
file
- the file to lock- Returns:
- a FileLock if an exclusive lock was obtained, null otherwise
-
unlockFile
Release anexclusive lock
on a file.- Parameters:
lock
- theexclusive lock
to release
-
separatorsToUnix
Converts all separators to the Unix separator of forward slash.- Parameters:
path
- the path to be changed, null ignored- Returns:
- the updated path
-
getPrefixLength
Returns the length of the filename prefix, such asC:/
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
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
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
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
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
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
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 isnull
.
-
removeExtension
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
-