Class Files

java.lang.Object
com.everdro1d.libs.io.Files

public final class Files extends Object
The Files class provides utility methods for file and directory operations.

This class includes methods for retrieving JAR file paths, checking file usage, deleting files, retrieving file names, opening files in the file manager, validating file paths, and loading or saving key-value maps to files.

Key Features:

  • Retrieve the absolute path or directory of a JAR file.
  • Check if a file is currently in use by attempting a write lock.
  • Delete files with optional debug output.
  • Retrieve all file names or matching file names in a directory.
  • Open files or directories in the system's default file manager.
  • Validate file paths for different operating systems.
  • Load and save key-value pairs from/to files in `key=value` format.

Note: This class cannot be instantiated as it is designed to be a utility class.

  • Method Details

    • getJarPath

      public static String getJarPath(Class<?> clazz)
      Retrieves the absolute path of the JAR file containing the specified class.
      Parameters:
      clazz - the class whose JAR file path is to be determined
      Returns:
      the absolute path of the JAR file as a string, or null if the path cannot be determined
    • getJarDirectory

      public static String getJarDirectory(Class<?> clazz)
      Retrieves the directory containing the JAR file of the specified class.
      Parameters:
      clazz - the class whose JAR file directory is to be determined
      Returns:
      the directory path of the JAR file as a string, or null if the path cannot be determined
    • isFileInUse

      public static boolean isFileInUse(Path filePath)
      Checks if a file is currently in use by attempting to acquire a write lock on it.
      Parameters:
      filePath - the path of the file to check
      Returns:
      true if the file is in use or an error occurs, false otherwise
    • deleteFile

      public static void deleteFile(String path)
      Deletes a file at the specified path.
      Parameters:
      path - the path of the file to delete
    • deleteFile

      public static void deleteFile(String path, boolean debug)
      Deletes a file at the specified path.
      Parameters:
      path - the path of the file to delete
      debug - if true, prints debug information to System.out about the deletion process
    • deleteDirectory

      public static void deleteDirectory(String path)
      Delete a directory and all files within.
      Parameters:
      path - path of the directory to delete
    • deleteDirectory

      public static void deleteDirectory(String path, boolean debug)
      Delete a directory and all files within.
      Parameters:
      path - path of the directory to delete
      debug - whether to print debug information
    • copyDirectory

      public static void copyDirectory(String source, String target) throws IOException
      Copy a directory and its complete file tree to the target location.
      Parameters:
      source - source directory
      target - target directory
      Throws:
      IOException - if an IOException is thrown by a visitor method in walkFileTree()
    • copyDirectory

      public static void copyDirectory(Path source, Path target) throws IOException
      Copy a directory and its complete file tree to the target location.
      Parameters:
      source - source directory
      target - target directory
      Throws:
      IOException - if an IOException is thrown by a visitor method in walkFileTree()
    • getMatchingFiles

      public static Set<String> getMatchingFiles(String inputDirectory, String contains)
      Retrieves a set of file names in a directory that contain a specific substring.
      Parameters:
      inputDirectory - the directory to search for files
      contains - the substring to match in file names
      Returns:
      a set of matching file names, or null if no matches are found
    • getAllFilesInDirectory

      public static Set<String> getAllFilesInDirectory(String inputDirectory)
      Retrieves a set of all file names in a specified directory.
      Parameters:
      inputDirectory - the directory to retrieve file names from
      Returns:
      a set of file names in the directory
    • getAllDirectoriesInDirectory

      public static Set<String> getAllDirectoriesInDirectory(String inputDirectory)
      Retrieves a set of all directory names in a specified directory.
      Parameters:
      inputDirectory - the directory to retrieve directory names from
      Returns:
      a set of directory names in the directory
    • openInFileManager

      public static void openInFileManager(String path)
      Opens a directory in the default file manager and selects the specified file.
      Parameters:
      path - the path to the file or directory to open
    • validateFilePath

      public static boolean validateFilePath(String path)
      Validates whether a given file path is valid and exists.
      Parameters:
      path - the file path to validate
      Returns:
      true if the path is valid and exists, false otherwise
    • loadMapFromFile

      public static Map<String,String> loadMapFromFile(String filePath)
      Loads a map from a file where each line represents a key-value pair in the format `key=value`.

      If the file does not exist or is empty, an error message is printed to System.err, and null is returned.

      Parameters:
      filePath - the full path of the file to load the map from
      Returns:
      a Map<String, String> containing the key-value pairs from the file, or null if the file does not exist or is empty
      Throws:
      RuntimeException - if an error occurs while reading the file

      Example:

       // File content:
       // key1=value1
       // key2=value2
      
       Map<String, String> map = Files.loadMapFromFile("/path/to/example.txt");
       System.out.println(map); // {key1=value1, key2=value2}
       
    • loadMapFromFile

      public static Map<String,String> loadMapFromFile(Path filePath)
      Loads a map from a file where each line represents a key-value pair in the format `key=value`.

      If the file does not exist or is empty, an error message is printed to System.err, and null is returned.

      Parameters:
      filePath - the full path of the file to load the map from
      Returns:
      a Map<String, String> containing the key-value pairs from the file, or null if the file does not exist or is empty
      Throws:
      RuntimeException - if an error occurs while reading the file

      Example:

       // File content:
       // key1=value1
       // key2=value2
      
       Map<String, String> map = Files.loadMapFromFile("/path/to/example.txt");
       System.out.println(map); // {key1=value1, key2=value2}
       
    • saveMapToFile

      public static void saveMapToFile(Path filePath, Map<String,String> map, boolean overwrite)
      Saves a map to a file in the format `key=value` with each entry on a new line.

      If the file already exists and overwrite is disabled, the method stops and prints an error message to System.err. If overwrite is enabled, the existing file is deleted and replaced with the new content.

      Parameters:
      filePath - the full filepath where the file will be saved (some/dir/filename.txt)
      map - the Map<String, String> to save
      overwrite - whether to overwrite the file if it already exists
      Throws:
      RuntimeException - if an error occurs while writing to the file

      Example:

       Map<String, String> map = new HashMap<>();
       map.put("key1", "value1");
       map.put("key2", "value2");
      
       Files.saveMapToFile("/path/to/directory/file.txt", map, true);
       // File content:
       // key1=value1
       // key2=value2
       
      See Also:
    • saveMapToFile

      public static void saveMapToFile(String path, String fileName, Map<String,String> map, boolean overwrite)
      Saves a map to a file in the format `key=value` with each entry on a new line.

      If the file already exists and overwrite is disabled, the method stops and prints an error message to System.err. If overwrite is enabled, the existing file is deleted and replaced with the new content.

      Parameters:
      path - the directory path where the file will be saved
      fileName - the name of the file (without extension) to save the map to
      map - the Map<String, String> to save
      overwrite - whether to overwrite the file if it already exists
      Throws:
      RuntimeException - if an error occurs while writing to the file

      Example:

       Map<String, String> map = new HashMap<>();
       map.put("key1", "value1");
       map.put("key2", "value2");
      
       Files.saveMapToFile("/path/to/directory", "example", map, true);
       // File content:
       // key1=value1
       // key2=value2
       
      See Also: