Class Files
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 Summary
Modifier and TypeMethodDescriptionstatic void
copyDirectory
(String source, String target) Copy a directory and its complete file tree to the target location.static void
copyDirectory
(Path source, Path target) Copy a directory and its complete file tree to the target location.static void
deleteDirectory
(String path) Delete a directory and all files within.static void
deleteDirectory
(String path, boolean debug) Delete a directory and all files within.static void
deleteFile
(String path) Deletes a file at the specified path.static void
deleteFile
(String path, boolean debug) Deletes a file at the specified path.getAllDirectoriesInDirectory
(String inputDirectory) Retrieves a set of all directory names in a specified directory.getAllFilesInDirectory
(String inputDirectory) Retrieves a set of all file names in a specified directory.static String
getJarDirectory
(Class<?> clazz) Retrieves the directory containing the JAR file of the specified class.static String
getJarPath
(Class<?> clazz) Retrieves the absolute path of the JAR file containing the specified class.getMatchingFiles
(String inputDirectory, String contains) Retrieves a set of file names in a directory that contain a specific substring.static boolean
isFileInUse
(Path filePath) Checks if a file is currently in use by attempting to acquire a write lock on it.loadMapFromFile
(String filePath) Loads a map from a file where each line represents a key-value pair in the format `key=value`.loadMapFromFile
(Path filePath) Loads a map from a file where each line represents a key-value pair in the format `key=value`.static void
openInFileManager
(String path) Opens a directory in the default file manager and selects the specified file.static void
Saves a map to a file in the format `key=value` with each entry on a new line.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.static boolean
validateFilePath
(String path) Validates whether a given file path is valid and exists.
-
Method Details
-
getJarPath
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
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
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
Deletes a file at the specified path.- Parameters:
path
- the path of the file to delete
-
deleteFile
Deletes a file at the specified path.- Parameters:
path
- the path of the file to deletedebug
- iftrue
, prints debug information to System.out about the deletion process
-
deleteDirectory
Delete a directory and all files within.- Parameters:
path
- path of the directory to delete
-
deleteDirectory
Delete a directory and all files within.- Parameters:
path
- path of the directory to deletedebug
- whether to print debug information
-
copyDirectory
Copy a directory and its complete file tree to the target location.- Parameters:
source
- source directorytarget
- target directory- Throws:
IOException
- if an IOException is thrown by a visitor method in walkFileTree()
-
copyDirectory
Copy a directory and its complete file tree to the target location.- Parameters:
source
- source directorytarget
- target directory- Throws:
IOException
- if an IOException is thrown by a visitor method in walkFileTree()
-
getMatchingFiles
Retrieves a set of file names in a directory that contain a specific substring.- Parameters:
inputDirectory
- the directory to search for filescontains
- the substring to match in file names- Returns:
- a set of matching file names, or
null
if no matches are found
-
getAllFilesInDirectory
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
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
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
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
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
, andnull
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, ornull
if the file does not exist or is empty - Throws:
RuntimeException
- if an error occurs while reading the fileExample:
// 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
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
, andnull
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, ornull
if the file does not exist or is empty - Throws:
RuntimeException
- if an error occurs while reading the fileExample:
// 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
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
- theMap<String, String>
to saveoverwrite
- whether to overwrite the file if it already exists- Throws:
RuntimeException
- if an error occurs while writing to the fileExample:
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 savedfileName
- the name of the file (without extension) to save the map tomap
- theMap<String, String>
to saveoverwrite
- whether to overwrite the file if it already exists- Throws:
RuntimeException
- if an error occurs while writing to the fileExample:
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:
-