Class FileUtils
- java.lang.Object
-
- io.microsphere.io.FileUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.io.File[]
EMPTY_FILE_ARRAY
An empty immutableFile
array.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
cleanDirectory(java.io.File directory)
Cleans a directory by deleting all files and sub-directories without deleting the directory itself.static int
deleteDirectory(java.io.File directory)
Deletes a directory and returns the number of deleted files and directories.static void
deleteDirectoryOnExit(java.io.File directory)
Schedules a directory for deletion on JVM exit, including all its contents.static int
forceDelete(java.io.File file)
Deletes a file or directory and all its contents recursively.static void
forceDeleteOnExit(java.io.File file)
Schedules a file or directory for deletion on JVM exit.static java.io.File
getCanonicalFile(java.io.File file)
Returns the canonical form of the specifiedFile
.static java.lang.String
getFileExtension(java.lang.String fileName)
Gets the extension of a file name, if any.static boolean
isSymlink(java.io.File file)
Determines if the providedFile
is a symbolic link.static java.io.File[]
listFiles(java.io.File directory)
Lists the files in the specified directory.static java.lang.String
resolveRelativePath(java.io.File parentDirectory, java.io.File targetFile)
Resolves the relative path from a parent directory to a target file.
-
-
-
Field Detail
-
EMPTY_FILE_ARRAY
@Immutable public static final java.io.File[] EMPTY_FILE_ARRAY
An empty immutableFile
array.
-
-
Method Detail
-
resolveRelativePath
@Nullable public static java.lang.String resolveRelativePath(java.io.File parentDirectory, java.io.File targetFile)
Resolves the relative path from a parent directory to a target file.If the
targetFile
is not under the specifiedparentDirectory
, this method returnsnull
. If the paths are equal, an empty string is returned.Example Usage
resolveRelativePath(new File("/home/user"), new File("/home/user/docs/file.txt"))
returns"docs/file.txt"
resolveRelativePath(new File("/home/user"), new File("/home/user/file.txt"))
returns"file.txt"
resolveRelativePath(new File("/home/user"), new File("/tmp/file.txt"))
returnsnull
resolveRelativePath(new File("/home/user"), new File("/home/user"))
returns an empty string
- Parameters:
parentDirectory
- the base directory to calculate the relative path fromtargetFile
- the target file or directory whose relative path is to be determined- Returns:
- the relative path from the parent directory to the target file, using forward slashes (
/
), ornull
if the target file is not under the parent directory
-
getFileExtension
@Nullable public static java.lang.String getFileExtension(java.lang.String fileName)
Gets the extension of a file name, if any.Example Usage
getFileExtension("file.txt")
returns"txt"
getFileExtension("file.tar.gz")
returns"gz"
getFileExtension(".hiddenfile")
returnsnull
(no extension)getFileExtension("file")
returnsnull
(no extension)getFileExtension("")
returnsnull
(blank string)getFileExtension(null)
returnsnull
- Parameters:
fileName
- the name of the file, may benull
or blank- Returns:
- the file's extension without the dot (.), or
null
if there's no extension or input is blank
-
deleteDirectory
public static int deleteDirectory(java.io.File directory) throws java.io.IOException
Deletes a directory and returns the number of deleted files and directories.If the directory does not exist, it is considered already deleted, and this method returns 0.
Example Usage
deleteDirectory(new File("/tmp/testDir"))
deletes the directory and all its contents, returning the total count of deleted files and directories.deleteDirectory(new File("/nonexistent/dir"))
returns0
since the directory does not exist.
- Parameters:
directory
- the directory to delete, must not benull
- Returns:
- the number of deleted files and directories
- Throws:
java.lang.NullPointerException
- if the directory isnull
java.io.IOException
- in case deletion is unsuccessful
-
cleanDirectory
public static int cleanDirectory(java.io.File directory) throws java.io.IOException
Cleans a directory by deleting all files and sub-directories without deleting the directory itself.This method recursively deletes all files and directories within the provided directory. If any file or sub-directory cannot be deleted, an IOException is thrown after attempting to delete as many as possible.
1Example Usage
File dir = new File("/path/to/directory"); int deletedCount = cleanDirectory(dir); System.out.println("Deleted " + deletedCount + " files/directories.");
- Parameters:
directory
- the directory to clean, must not benull
- Returns:
- the number of deleted files and directories
- Throws:
java.lang.NullPointerException
- if the directory isnull
java.io.IOException
- if deletion fails for any file or sub-directory
-
forceDelete
public static int forceDelete(java.io.File file) throws java.io.IOException
Deletes a file or directory and all its contents recursively.If the provided
file
is a directory, this method deletes all sub-directories and files, then deletes the directory itself. If it's a regular file, it deletes that single file.Example Usage
forceDelete(new File("/tmp/file.txt"))
deletes the file and returns1
forceDelete(new File("/tmp/testDir"))
deletes the directory and all its contents, returning the total count of deleted files and directories.forceDelete(new File("/nonexistent/file"))
throws aFileNotFoundException
- Parameters:
file
- the file or directory to delete, must not benull
- Returns:
- the number of deleted files and directories
- Throws:
java.lang.NullPointerException
- if the file isnull
java.io.FileNotFoundException
- if the file does not existjava.io.IOException
- if deletion fails for any reason
-
forceDeleteOnExit
public static void forceDeleteOnExit(java.io.File file)
Schedules a file or directory for deletion on JVM exit.If the provided
file
is a directory, this method schedules all sub-directories and files, then schedules the directory itself. If it's a regular file, it schedules that single file.Example Usage
forceDeleteOnExit(new File("/tmp/file.txt"))
schedules the file for deletion on exit.forceDeleteOnExit(new File("/tmp/testDir"))
schedules the directory and all its contents for deletion on exit.
- Parameters:
file
- the file or directory to schedule for deletion, must not benull
- Throws:
java.lang.NullPointerException
- if the file isnull
-
deleteDirectoryOnExit
public static void deleteDirectoryOnExit(java.io.File directory)
Schedules a directory for deletion on JVM exit, including all its contents.If the directory does not exist, this method does nothing. If it does exist, it schedules the directory for deletion and recursively schedules all files and subdirectories for deletion.
Example Usage
deleteDirectoryOnExit(new File("/tmp/testDir"))
ensures that the directory and all its contents are deleted when the JVM exits.deleteDirectoryOnExit(new File("/nonexistent/dir"))
does nothing since the directory does not exist.
- Parameters:
directory
- the directory to schedule for deletion on exit, must not benull
- Throws:
java.lang.NullPointerException
- if the directory isnull
-
listFiles
@Nonnull public static java.io.File[] listFiles(java.io.File directory)
Lists the files in the specified directory.If the provided
directory
is not valid (i.e., it does not exist, or it is not a directory), this method returns an empty file array.Example Usage
listFiles(new File("/tmp"))
returns an array of files in the "/tmp" directory.listFiles(new File("/nonexistent/dir"))
returns an empty file array since the directory does not exist.listFiles(null)
returns an empty file array as the input is null.
- Parameters:
directory
- the directory to list files from- Returns:
- an array of
File
objects representing the files in the specified directory, orEMPTY_FILE_ARRAY
if the directory is not valid
-
isSymlink
public static boolean isSymlink(java.io.File file)
Determines if the providedFile
is a symbolic link.Example Usage
isSymlink(new File("/path/to/symlink"))
returnstrue
if it's a symbolic link.isSymlink(new File("/path/to/regularfile"))
returnsfalse
as it's not a symbolic link.isSymlink(null)
throws aNullPointerException
.
- Parameters:
file
- the file to check, must not benull
- Returns:
true
if the file is a symbolic link, otherwisefalse
- Throws:
java.lang.NullPointerException
- if the file isnull
-
getCanonicalFile
@Nonnull public static java.io.File getCanonicalFile(java.io.File file)
Returns the canonical form of the specifiedFile
.This method wraps the call to
File.getCanonicalFile()
in a try-catch block to handle any checked exceptions via theThrowableSupplier
utility.Example Usage
getCanonicalFile(new File("relative/path"))
returns the canonical file object.- If the file does not exist or I/O error occurs, it will propagate as an unchecked exception.
- Parameters:
file
- the file for which the canonical representation is required, must not benull
- Returns:
- the canonical representation of the given file
- Throws:
java.lang.NullPointerException
- if the provided file isnull
java.lang.RuntimeException
- if an I/O error occurs while retrieving the canonical file
-
-