Class FileUtil

java.lang.Object
nl.pim16aap2.animatedarchitecture.core.util.FileUtil

public final class FileUtil extends Object
Utility class for file operations.
  • Method Details

    • deleteFile

      public static void deleteFile(Path file)
      Deletes a file if it exists.

      If an exception occurs while deleting the file, it will be logged, but the method will not throw an exception.

      If the file does not exist, nothing happens.

      Parameters:
      file - The file to delete.
    • ensureFileExists

      public static Path ensureFileExists(Path file) throws IOException
      Ensures that a file exists.

      If the file does not already exist, it will be created.

      Parameters:
      file - The file whose existence to ensure.
      Returns:
      The path to the file.
      Throws:
      IOException - When the file could not be created.
    • appendToFile

      public static void appendToFile(Path path, List<String> append)
      Appends a list of strings to a file.

      Every entry in the list will be printed on its own line.

      Parameters:
      path - The path of the file.
      append - The list of Strings (lines) to append to the file.
    • ensureDirectoryExists

      public static void ensureDirectoryExists(Path dir)
      Ensures a directory exists.
    • ensureZipFileExists

      public static void ensureZipFileExists(Path zipFile)
      Ensures a given zip file exists.
    • getJarFile

      public static Path getJarFile(Class<?> clz)
      Gets the file of the jar that contained a specific class.
      Parameters:
      clz - The class for which to find the jar file.
      Returns:
      The location of the jar file.
    • createNewFileSystem

      public static FileSystem createNewFileSystem(Path zipFile) throws IOException, URISyntaxException, ProviderNotFoundException
      Creates a new FileSystem for a zip file.

      Don't forget to close it when you're done!

      Parameters:
      zipFile - The zip file for which to create a new FileSystem.
      Returns:
      The newly created FileSystem.
      Throws:
      IOException - If an I/O error occurs creating the file system.
      FileSystemAlreadyExistsException - When a FileSystem already exists for the provided file.
      URISyntaxException
      ProviderNotFoundException
    • getFilesInJar

      public static List<String> getFilesInJar(Path jarFile, Pattern pattern) throws IOException
      Gets the names of all files inside a jar that match a certain pattern.
      Parameters:
      jarFile - The jar file to search in.
      pattern - The pattern to match the file names against.
      Returns:
      The names of all files in the jar that match the pattern.
      Throws:
      IOException - If an I/O error occurs.