Class IOTools
Utility methods for common file system operations including existence checks,
executability checks, directory creation, and path resolution. All methods
accept null inputs and return false (or an appropriate default)
rather than throwing NullPointerException.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanExecute(@Nullable File file) Determines if the specified file exists, is a file, and is executable.static booleancanExecute(@Nullable String path) Determines if the file at the specified path string exists, is a regular file, and is executable.static booleancanExecute(@Nullable Path path) Determines if the specified path exists, is a regular file, and is executable.static booleancreateDirs(@Nullable File file) Creates the directory named by the given file, including any nonexistent parent directories.static booleancreateDirs(@Nullable String path) Creates the directory named by the given path string, including any nonexistent parent directories.static booleancreateDirs(@Nullable Path path) Creates the directory named by the given path, including any nonexistent parent directories.static booleanChecks if the specified file exists.static booleanChecks whether a file or directory exists at the specified path.static booleanChecks if the specified path exists.findFilesByExtensions(Path directory, String... extensions) Finds regular files located directly indirectorywhose file name ends with one of the given extensions (case-insensitive).static booleanisDirectory(@Nullable File file) Determines if the specifiedFileis a directory.static booleanisDirectory(@Nullable String path) Determines if the specified path string represents an existing directory.static booleanisDirectory(@Nullable Path path) Determines if the specifiedPathrepresents an existing directory.static booleanCreates the directory specified by the given file, including any nonexistent parent directories as necessary.static booleanCreates the directory specified by the given path string, including any nonexistent parent directories as necessary.static booleanCreates the directory specified by the given path, including any nonexistent parent directories as necessary.static booleanChecks if the specified file does not exist.static booleanChecks whether a file or directory does not exist at the specified path.static booleanChecks if the specified path does not exist.static FileResolves a file path by joining a base file with additional path segments.
-
Method Details
-
canExecute
Determines if the specified file exists, is a file, and is executable.- Parameters:
file- the file to be checked- Returns:
trueif the file exists, is a file, and can be executed;falseotherwise- Since:
- 1.0
-
canExecute
Determines if the specified path exists, is a regular file, and is executable.- Parameters:
path- the path to be checked- Returns:
trueif the path exists, is a regular file, and can be executed;falseotherwise- Since:
- 1.0
-
canExecute
Determines if the file at the specified path string exists, is a regular file, and is executable.- Parameters:
path- the path string to be checked- Returns:
trueif the path exists, is a regular file, and can be executed;falseotherwise, including when the path isnull, blank, or invalid- Since:
- 1.0
-
createDirs
Creates the directory named by the given path, including any nonexistent parent directories.If
pathisnull, this method does nothing and returnsfalse. If the directory already exists, this method does nothing and returnstrue.- Parameters:
path- the directory to create, may benull- Returns:
trueif the directory exists after the call,falseifpathisnull- Throws:
FileAlreadyExistsException- ifpathexists and is not a directoryAccessDeniedException- if the process does not have permission to create the directoryIOException- if an I/O error occurs while creating the directorySecurityException- if a security manager denies write access- Since:
- 1.3
-
createDirs
Creates the directory named by the given file, including any nonexistent parent directories.If
fileisnull, this method does nothing and returnsfalse.- Parameters:
file- the directory to create, may benull- Returns:
trueif the directory exists after the call,falseiffileisnull- Throws:
FileAlreadyExistsException- iffileexists and is not a directoryAccessDeniedException- if the process does not have permission to create the directoryIOException- if an I/O error occurs while creating the directorySecurityException- if a security manager denies write access- Since:
- 1.3
-
createDirs
Creates the directory named by the given path string, including any nonexistent parent directories.If
pathisnullor blank, this method does nothing and returnsfalse.- Parameters:
path- the path string of the directory to create, may benullor blank- Returns:
trueif the directory exists after the call,falseifpathisnullor blank- Throws:
InvalidPathException- ifpathcannot be converted to aPathFileAlreadyExistsException- ifpathexists and is not a directoryAccessDeniedException- if the process does not have permission to create the directoryIOException- if an I/O error occurs while creating the directorySecurityException- if a security manager denies write access- Since:
- 1.3
-
exists
Checks if the specified file exists.- Parameters:
file- the file to check for existence- Returns:
trueif the file is notnulland exists;falseotherwise- Since:
- 1.0
-
exists
Checks if the specified path exists.- Parameters:
path- the path to check for existence- Returns:
trueif the path is notnulland exists;falseotherwise- Since:
- 1.0
-
exists
Checks whether a file or directory exists at the specified path.- Parameters:
path- the file system path to check for existence- Returns:
trueif the path is notnulland a file or directory exists at the specified path;falseotherwise, including when the path is blank or invalid- Since:
- 1.0
-
findFilesByExtensions
Finds regular files located directly indirectorywhose file name ends with one of the given extensions (case-insensitive).This is non-recursive - it does not search subdirectories. If the directory cannot be read, an empty list is returned.
- Parameters:
directory- the directory to list, must not be nullextensions- one or more extensions to match, e.g. ".java" or "java"- Returns:
- an unmodifiable list of matching files; never null, may be empty
- Throws:
NullPointerException- if directory or extensions is null- Since:
- 1.4
-
isDirectory
Determines if the specifiedFileis a directory.- Parameters:
file- theFileobject to be checked; ifnull, returnsfalse- Returns:
trueif the file exists and is a directory;falseotherwise- Since:
- 1.0
-
isDirectory
Determines if the specifiedPathrepresents an existing directory.- Parameters:
path- thePathobject to be checked; ifnull, returnsfalse- Returns:
trueif the path exists and is a directory;falseotherwise- Since:
- 1.0
-
isDirectory
Determines if the specified path string represents an existing directory.- Parameters:
path- the path string to be checked; ifnullor blank, returnsfalse- Returns:
trueif the specified path exists and is a directory;falseotherwise, including when the path string is invalid- Since:
- 1.0
-
mkdirs
Creates the directory specified by the given file, including any nonexistent parent directories as necessary.Unlike
createDirs(File), this method catches all exceptions and returnsfalseon failure instead of throwing.- Parameters:
file- the directory to be created- Returns:
trueif the directory was created successfully or already exists;falseif the directory could not be created orfileisnull- Since:
- 1.0
-
mkdirs
Creates the directory specified by the given path, including any nonexistent parent directories as necessary.Unlike
createDirs(Path), this method catches all exceptions and returnsfalseon failure instead of throwing.- Parameters:
path- the directory to be created- Returns:
trueif the directory was created successfully or already exists;falseif the directory could not be created orpathisnull- Since:
- 1.0
-
mkdirs
Creates the directory specified by the given path string, including any nonexistent parent directories as necessary.Unlike
createDirs(String), this method catches all exceptions and returnsfalseon failure instead of throwing.- Parameters:
path- the directory to be created- Returns:
trueif the directory was created successfully or already exists;falseif the directory could not be created orpathisnull, blank, or invalid- Since:
- 1.0
-
notExists
Checks if the specified file does not exist.Note: This method returns
truefor bothnullinput and non-existent files. This diverges fromFiles.notExists(Path, java.nio.file.LinkOption...)which returnsfalsewhen existence cannot be determined. The behavior here is a deliberate choice to simplify null-checking call sites.- Parameters:
file- the file to check for non-existence- Returns:
trueif the file isnullor does not exist;falseotherwise- Since:
- 1.0
-
notExists
Checks if the specified path does not exist.Note: This method returns
truefor bothnullinput and non-existent paths. This diverges fromFiles.notExists(Path, java.nio.file.LinkOption...)which returnsfalsewhen existence cannot be determined. The behavior here is a deliberate choice to simplify null-checking call sites.- Parameters:
path- the path to check for non-existence- Returns:
trueif the path isnullor does not exist;falseotherwise- Since:
- 1.0
-
notExists
Checks whether a file or directory does not exist at the specified path.Note: This method returns
truefornull, blank, invalid, or non-existent paths. This diverges fromFiles.notExists(Path, java.nio.file.LinkOption...)which returnsfalsewhen existence cannot be determined. The behavior here is a deliberate choice to simplify null-checking call sites.- Parameters:
path- the file system path to check for non-existence- Returns:
trueif the path isnullor no file or directory exists at the specified path;falseotherwise- Since:
- 1.0
-
resolveFile
Resolves a file path by joining a base file with additional path segments.This method constructs a file path by appending one or more path segments to a base file.
nullor empty segments are silently skipped. To keep resolution relative tobase, segments starting with"/"have the leading slash stripped before resolving. This is a deliberate design choice that prevents absolute segments from resetting the path to the filesystem root, which would violate the expectation that resolution is relative to the base. Callers passing absolute Unix paths should pre-strip the slash themselves if they intend root-relative semantics.If
baseisnull, this behaves likenew File(""): segments are resolved against the current directory.- Parameters:
base- the base file path to start from; may benullsegments- additional path segments to append, in order; may benull, and individualnullor empty segments are silently skipped- Returns:
- a
Filerepresenting the resolved path - Since:
- 1.0
-