Class FileUtils

java.lang.Object
net.luis.utils.io.FileUtils

public final class FileUtils extends Object
Utility class for file operations.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    create(@NotNull Path file)
    Creates the given file and all parent directories.
    static void
    createIfNotExists(@NotNull Path file)
    Creates the given file and all parent directories if they do not exist.
    static @NotNull Path
    createSessionDirectory(@Nullable String prefix)
    Creates a temporary directory for the current session.
    The directory and all its content will be deleted when the JVM shuts down.
    static void
    deleteRecursively(@NotNull Path path)
    Deletes the given file or directory recursively.
    static @NotNull String
    getExtension(@Nullable String file)
    Gets the extension of the given file.
    The file can be a path or a file name.
    static @NotNull String
    getName(@Nullable String file)
    Gets the name of the given file.
    The file can be a path or a file name.
    static @NotNull String
    getRelativePath(@Nullable String file)
    Gets the relative path of the given file.
    The file can be a path or a file name.
    static @NotNull String
    readString(@NotNull Reader reader)
    Reads the content of the given reader as a string.
    static @NotNull String
    readString(@NotNull InputProvider provider)
    Reads the content of the given input provider as a string.
    static @NotNull String
    readString(@NotNull InputProvider provider, @NotNull Charset charset)
    Reads the content of the given input provider as a string using the given charset.
    static @NotNull Pair<String,String>
    split(@Nullable String file)
    Splits the given file into a pair of the folder and the file name.
    The folder is the part before the last slash, the name is the part after the last slash.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileUtils

      private FileUtils()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • split

      @NotNull public static @NotNull Pair<String,String> split(@Nullable @Nullable String file)
      Splits the given file into a pair of the folder and the file name.
      The folder is the part before the last slash, the name is the part after the last slash.

      Examples:

      
       split(null) -> ("", "")
       split("") -> ("", "")
       split(".") -> (".", "")
       split("./") -> ("./", "")
       split("foo") -> ("foo", "")
       split("/foo/") -> ("/foo"/, "")
       split("/foo.json") -> ("/", "foo.json")
       split("/bar/foo.json") -> ("/bar"/, "foo.json")
       
      Parameters:
      file - The file to split as a string
      Returns:
      The pair of the folder and the file name
    • getName

      @NotNull public static @NotNull String getName(@Nullable @Nullable String file)
      Gets the name of the given file.
      The file can be a path or a file name.

      Examples:

      
       getName(null) -> ""
       getName("") -> ""
       getName("/") -> ""
       getName("/foo.json") -> "foo"
       getName("/bar/foo.json") -> "foo"
       
      Parameters:
      file - The file to get the name of
      Returns:
      The name of the file or an empty string if the file has no name
    • getExtension

      @NotNull public static @NotNull String getExtension(@Nullable @Nullable String file)
      Gets the extension of the given file.
      The file can be a path or a file name.

      Examples:

      
       getExtension(null) -> ""
       getExtension("") -> ""
       getExtension("/") -> ""
       getExtension("/foo.json") -> "json"
       getExtension("/bar/foo.json") -> "json"
       
      Parameters:
      file - The file to get the extension of
      Returns:
      The extension of the file or an empty string if the file has no extension
    • getRelativePath

      @NotNull public static @NotNull String getRelativePath(@Nullable @Nullable String file)
      Gets the relative path of the given file.
      The file can be a path or a file name.

      Examples:

      
       getRelativePath(null) -> "./"
       getRelativePath("") -> "./"
       getRelativePath("/") -> "./"
       getRelativePath("foo") -> "./foo/"
       getRelativePath("./foo/") -> "./foo/"
       getRelativePath("/foo.json") -> "./foo.json"
       getRelativePath("/bar/foo.json") -> "./bar/foo.json"
       
      Parameters:
      file - The file to get the relative path of
      Returns:
      The relative path of the file
    • create

      @Blocking public static void create(@NotNull @NotNull Path file) throws IOException
      Creates the given file and all parent directories.
      Parameters:
      file - The file to create
      Throws:
      NullPointerException - If the file is null
      FileAlreadyExistsException - If the file already exists
      IOException - If an I/O error occurs
    • createIfNotExists

      @Blocking public static void createIfNotExists(@NotNull @NotNull Path file) throws IOException
      Creates the given file and all parent directories if they do not exist.
      Parameters:
      file - The file to create
      Throws:
      NullPointerException - If the file is null
      IOException - If an I/O error occurs
      See Also:
    • createSessionDirectory

      @Blocking @NotNull public static @NotNull Path createSessionDirectory(@Nullable @Nullable String prefix) throws IOException
      Creates a temporary directory for the current session.
      The directory and all its content will be deleted when the JVM shuts down.
      Parameters:
      prefix - The prefix of the directory name
      Returns:
      The path of the created directory
      Throws:
      IOException - If an I/O error occurs
      See Also:
    • deleteRecursively

      @Blocking public static void deleteRecursively(@NotNull @NotNull Path path) throws IOException
      Deletes the given file or directory recursively.
      Parameters:
      path - The path to delete
      Throws:
      NullPointerException - If the path is null
      IOException - If an I/O error occurs
    • readString

      @Blocking @NotNull public static @NotNull String readString(@NotNull @NotNull InputProvider provider) throws IOException
      Reads the content of the given input provider as a string.
      Parameters:
      provider - The input provider to read from
      Returns:
      The content of the input provider
      Throws:
      NullPointerException - If the provider is null
      IOException - If an I/O error occurs
    • readString

      @Blocking @NotNull public static @NotNull String readString(@NotNull @NotNull InputProvider provider, @NotNull @NotNull Charset charset) throws IOException
      Reads the content of the given input provider as a string using the given charset.
      Parameters:
      provider - The input provider to read from
      charset - The charset to use
      Returns:
      The content of the input provider
      Throws:
      NullPointerException - If the provider or charset is null
      IOException - If an I/O error occurs
    • readString

      @Blocking @NotNull public static @NotNull String readString(@NotNull @NotNull Reader reader) throws IOException
      Reads the content of the given reader as a string.
      Parameters:
      reader - The reader to read from
      Returns:
      The content of the reader
      Throws:
      NullPointerException - If the reader is null
      IOException - If an I/O error occurs