Class FileUtil

java.lang.Object
jp.ecuacion.lib.core.util.FileUtil

public class FileUtil extends Object
Provides File-related utility methods.
  • Method Details

    • getFileSavableName

      @Nonnull public static String getFileSavableName(@RequireNonnull String origName)
      Changes argument filename into file-savable name.
    • concatFilePaths

      @Nonnull public static String concatFilePaths(@Nonnull String... paths)
      Concatenates file paths.
      Parameters:
      paths - paths
      Returns:
      the concatenated path
    • cleanPathStrWithSlash

      @Nonnull public static String cleanPathStrWithSlash(@RequireNonnull String path)
      Cleans a path string.

      Paths like /path//to\file change 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

      @Nonnull public static boolean containsWildCard(@RequireNonnull String path)
      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

      @Nonnull public static String getParentDirPath(@RequireNonnull String origPath)
      Changes the path separator to "/".
      Parameters:
      origPath - original path
      Returns:
      the separator changed path
    • getFileNameFromFilePath

      @Nonnull public static String getFileNameFromFilePath(@RequireNonnull String path)
      Obtains filename from a path.

      Both "/" and "\" are treated as the separator.

      Parameters:
      path - path
      Returns:
      filename
    • getFileSizeInMb

      @Nonnull public static String getFileSizeInMb(@RequireNonnull Long fileSize)
      Returns file size in Megabyte.
      Parameters:
      fileSize - fileSize
      Returns:
      the file size in Megabyte
    • getFileSizeInMbWithUnit

      @Nonnull public static String getFileSizeInMbWithUnit(@RequireNonnull Long fileSize)
      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 lockFile is 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 by getLockFileVersion method.
      When the lock is released, the lock file is updated by writing the timestamp to it.

      Parameters:
      lockFile - lockFile
      version - the version, may be null when you don't have to validate the version from the optimistic exclusive control.
      Returns:
      The Pair tuple which have FileChannel and FileLock.
      Throws:
      IOException - IOException
    • isLocked

      public static boolean isLocked(@RequireNonnull String path) throws IOException
      Checks if file is locked.
      Parameters:
      path - path, Both absolute and relativve path is acceptable.
      Returns:
      true when 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 method lock.
      Throws:
      IOException - IOException
    • getLockFileVersion

      @Nonnull public static String getLockFileVersion(@RequireNonnull File lockFile) throws IOException
      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