public class FileHelper extends Object
Constructor and Description |
---|
FileHelper() |
Modifier and Type | Method and Description |
---|---|
static void |
copyDir(File fileDirFrom, File fileDirTo)
Create a deep copy of the specified directory into the specified target directory.
|
static void |
copyFile(File fileFrom, File fileTo)
Copy the specified file according to the source and destination File objects preserving the time-stamp of the source.
|
static File |
createTempDir()
Create a unique temporary directory.
|
static void |
deleteDir(File fileDir)
Delete a directory recursively.
|
static void |
deleteDirSilent(File fileDir)
Recursively delete a directory suppressing any raised exceptions.
|
static File |
ensureDir(File 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 |
getName(String filename)
Gets the name minus the path from a full filename.
|
static String |
getPath(File file)
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 as
C:/ 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 |
lockFile(File file)
Obtain an exclusive lock on the specified file.
|
static void |
moveDir(File fileDirFrom, File fileDirTo)
Move the specified directory to the specified destination path.
|
static void |
moveFile(File fileFrom, File fileTo)
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 |
sizeDir(File fileDir)
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 an
exclusive lock on a file. |
static void |
validateDir(File file)
Validate that the given File exists and represents a directory.
|
public static String toFilename(String sName)
sName
- the stringpublic static String getPath(File file)
The implementation attempts to get the canonical path and iff this call abruptly fails is the absolute path returned.
file
- the File
to be interrogated to return the pathpublic static File ensureDir(File file) throws IOException
file
- the File to checkIOException
- on error creating the directorypublic static void validateDir(File file) throws IOException
file
- the File to checkIOException
- if the given File doesn't exist or doesn't represent a directorypublic static void copyDir(File fileDirFrom, File fileDirTo) throws IOException
fileDirFrom
- the directory to copy fromfileDirTo
- the directory to copy intoIOException
- if an error occurred copying the directorypublic static void deleteDir(File fileDir) throws IOException
fileDir
- directory to deleteIOException
- if an error occurred deleting the directorypublic static void deleteDirSilent(File fileDir)
fileDir
- directory to deletepublic static void moveDir(File fileDirFrom, File fileDirTo) throws IOException
fileDirFrom
- the directory to movefileDirTo
- the destination pathIOException
- if an error occurred moving the directorypublic static File createTempDir() throws IOException
IOException
public static long sizeDir(File fileDir)
fileDir
- the directory to sizepublic static void copyFile(File fileFrom, File fileTo) throws IOException
fileFrom
- the File
to copy fromfileTo
- the File
to copy toIOException
- if an error occurred copying the filepublic static void moveFile(File fileFrom, File fileTo) throws IOException
fileFrom
- the file to movefileTo
- the destination pathIOException
- if an error occurred moving the filepublic static FileLock lockFile(File file)
unlockFile
method.file
- the file to lockpublic static void unlockFile(FileLock lock)
exclusive lock
on a file.lock
- the exclusive lock
to releasepublic static String separatorsToUnix(String path)
path
- the path to be changed, null ignoredpublic static int getPrefixLength(String filename)
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.
filename
- the filename to find the prefix in, null returns -1public static int indexOfLastSeparator(String filename)
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.
filename
- the filename to find the last path separator in, null returns -1public static int indexOfExtension(String filename)
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.
filename
- the filename to find the last extension separator in, null returns -1public static String getPathNoEndSeparator(String filename)
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.
filename
- the filename to query, null returns nullpublic static String getName(String 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.
filename
- the filename to query, null returns nullpublic static String getBaseName(String 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.
filename
- the filename to query, null returns nullpublic static String getExtension(String 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.
filename
- the filename to retrieve the extension of.null
if the filename is null
.public static String removeExtension(String 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.
filename
- the filename to query, null returns null