Class Files
java.lang.Object
com.everdro1d.libs.io.Files
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 Summary
Modifier and TypeMethodDescriptionstatic void
deleteFile
(String path, boolean debug) Deletes a file at the specified path.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 fileName) 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 boolean
validateFilePath
(String path) Validates whether a given file path is valid and exists.
-
Method Details
-
getJarPath
-
getJarDirectory
-
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 deletedebug
- iftrue
, prints debug information to System.out about the deletion process
-
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
-
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:
fileName
- the name 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("example.txt"); System.out.println(map); // {key1=value1, key2=value2}
-
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
-