Class ResourceUtils

java.lang.Object
me.croabeast.file.ResourceUtils

public class ResourceUtils extends Object
The ResourceUtils class provides utility methods for handling file resources, such as saving resources to the file system and constructing file paths.

It contains methods to save an InputStream resource to a specified location within a data folder and to resolve file paths relative to a parent directory.

See Also:
  • Constructor Details

    • ResourceUtils

      public ResourceUtils()
  • Method Details

    • saveResource

      public void saveResource(@Nullable @Nullable InputStream resource, File dataFolder, String path, boolean replace)
      Saves the specified resource to the given file path within the data folder.

      If the target directory does not exist, it will be created. If a file already exists at the target location and replace is false, an UnsupportedOperationException is thrown.

      Parameters:
      resource - the input stream of the resource to save; must not be null.
      dataFolder - the data folder where the resource will be saved; must not be null.
      path - the file path within the data folder to save the resource to; must not be null.
      replace - whether to replace the existing file if it already exists.
      Throws:
      NullPointerException - if the path or resource is null.
      UnsupportedOperationException - if the file already exists and replace is false, or if an IO error occurs during saving.
    • saveResource

      public void saveResource(@Nullable @Nullable InputStream resource, File dataFolder, String path)
      Saves the specified resource to the given file path within the data folder.

      This method behaves like saveResource(InputStream, File, String, boolean) with replace set to false. Existing files will not be replaced.

      Parameters:
      resource - the input stream of the resource to save; must not be null.
      dataFolder - the data folder where the resource will be saved; must not be null.
      path - the file path within the data folder to save the resource to; must not be null.
      Throws:
      NullPointerException - if the path or resource is null.
      UnsupportedOperationException - if the file already exists or if an IO error occurs during saving.
    • fileFrom

      public File fileFrom(@Nullable @Nullable File parent, String... childPaths)
      Creates a File object by resolving the specified child file paths against the given parent directory.

      The method iteratively appends each element in childPaths to the parent directory, producing a final File that represents the nested file location.

      Parameters:
      parent - the parent directory to resolve the child file paths against; must not be null.
      childPaths - the relative paths of the child files.
      Returns:
      the File object representing the specified child file paths within the parent directory.
      Throws:
      NullPointerException - if the parent directory is null.