Class FileUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcleanPathStrWithSlash(String path) Cleans a path string.static StringconcatFilePaths(String... paths) Concatenates file paths.static booleancontainsWildCard(String path) Returns true if the argument path contains wildcard strings.static StringObtains filename from a path.static StringgetFileSavableName(String origName) Changes argument filename into file-savable name.static StringgetFileSizeInMb(Long fileSize) Returns file size in Megabyte.static StringgetFileSizeInMbWithUnit(Long fileSize) Returns file size in Megabyte.static StringgetLockFileVersion(File lockFile) Obtains the last update timestamp string from the lock file.static StringgetParentDirPath(String origPath) Changes the path separator to "/".Returns a list of paths which match the path passed by the argument path with wildcards.static booleanChecks if file is locked.static booleanisRelativePath(String path) Returns true if the path is relative.static org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> Obtains the lock of the designated file.static voidrelease(org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> channelAndLock) Releases the lock.
-
Method Details
-
getFileSavableName
Changes argument filename into file-savable name. -
concatFilePaths
-
cleanPathStrWithSlash
Cleans a path string.Paths like
/path//to\filechange to the clean format like/path/to/file.
Separator is always/even if this method is called in Windows OS.- Parameters:
path- path- Returns:
- the cleaned path
-
containsWildCard
Returns true if the argument path contains wildcard strings. -
getPathListFromPathWithWildcard
@Nonnull public static List<String> getPathListFromPathWithWildcard(@RequireNonnull String path) throws BizLogicAppException Returns a list of paths which match the path passed by the argument path with wildcards."*", "?" are supported, but "**" not supported.
The separator of returning Paths is "/"- Throws:
BizLogicAppException
-
isRelativePath
@Nonnull public static boolean isRelativePath(@RequireNonnull String path) throws BizLogicAppException Returns true if the path is relative.- Parameters:
path- path- Returns:
- true if the path is relative
- Throws:
BizLogicAppException- BizLogicAppException
-
getParentDirPath
Changes the path separator to "/".- Parameters:
origPath- original path- Returns:
- the separator changed path
-
getFileNameFromFilePath
Obtains filename from a path.Both "/" and "\" are treated as the separator.
- Parameters:
path- path- Returns:
- filename
-
getFileSizeInMb
Returns file size in Megabyte.- Parameters:
fileSize- fileSize- Returns:
- the file size in Megabyte
-
getFileSizeInMbWithUnit
Returns file size in Megabyte.- Parameters:
fileSize- fileSize- Returns:
- the file size in Megabyte
-
lock
@Nonnull public static org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> lock(@RequireNonnull File lockFile, @Nullable String version) throws IOException Obtains the lock of the designated file.The argument
lockFileis the file only for locks, not the business use files.This method also supports the optimistic exclusive control.
The version is obtained from the timestamp string of the file bygetLockFileVersionmethod.
When the lock is released, the lock file is updated by writing the timestamp to it.- Parameters:
lockFile- lockFileversion- the version, may benullwhen you don't have to validate the version from the optimistic exclusive control.- Returns:
- The
Pairtuple which haveFileChannelandFileLock. - Throws:
IOException- IOException
-
isLocked
Checks if file is locked.- Parameters:
path- path, Both absolute and relativve path is acceptable.- Returns:
truewhen file is locked.- Throws:
IOException- IOException
-
release
public static void release(@RequireNonnull org.apache.commons.lang3.tuple.Pair<FileChannel, FileLock> channelAndLock) throws IOException Releases the lock.The timestamp string is written to the lock file right before the lock is released.
- Parameters:
channelAndLock- the return object of the methodlock.- Throws:
IOException- IOException
-
getLockFileVersion
Obtains the last update timestamp string from the lock file.This is used to get the version for optimistic exclusive control.
If the file does not exist, this method creates it.
- Returns:
- The timestamp string in yyyy-mm-dd-hh-mi-ss.SSS format. To ignore the time offset, the time is always treated as UTC.
- Throws:
IOException
-