import java.io.File;
import java.io.IOException;
public class DirectoryManager {
/**
* Creates a directory, handling potential exceptions and ensuring defensive behavior.
* @param path The path to the directory to create.
* @return True if the directory was created successfully, false otherwise.
*/
public static boolean createDirectory(String path) {
File dir = new File(path);
// Defensive check: Check if the path is null or empty.
if (path == null || path.trim().isEmpty()) {
System.err.println("Error: Path cannot be null or empty.");
return false;
}
// Defensive check: Check if the parent directory exists.
File parentDir = dir.getParentFile();
if (parentDir != null && !parentDir.exists()) {
System.err.println("Error: Parent directory does not exist.");
return false;
}
try {
if (dir.mkdir()) {
System.out.println("Directory created successfully: " + path);
return true;
} else {
System.err.println("Error: Failed to create directory: " + path);
return false;
}
} catch (SecurityException e) {
System.err.println("Error: Insufficient permissions to create directory: " + path);
e.printStackTrace();
return false;
} catch (Exception e) {
System.err.println("Error creating directory: " + path + ". " + e.getMessage());
e.printStackTrace();
return false;
}
}
/**
* Checks if a directory exists, handling potential exceptions.
* @param path The path to the directory.
* @return True if the directory exists, false otherwise.
*/
public static boolean directoryExists(String path) {
File dir = new File(path);
if (path == null || path.trim().isEmpty()) {
System.err.println("Error: Path cannot be null or empty.");
return false;
}
return dir.exists() && dir.isDirectory();
}
/**
* Deletes a directory and its contents recursively, with defensive checks.
* @param path The path to the directory to delete.
* @return True if the directory was deleted successfully, false otherwise.
*/
public static boolean deleteDirectory(String path) {
File dir = new File(path);
if (path == null || path.trim().isEmpty()) {
System.err.println("Error: Path cannot be null or empty.");
return false;
}
if (!dir.exists() || !dir.isDirectory()) {
System.err.println("Error: Directory does not exist or is not a directory: " + path);
return false;
}
try {
if (dir.delete()) {
System.out.println("Directory deleted successfully: " + path);
return true;
} else {
System.err.println("Error: Failed to delete directory: " + path);
return false;
}
} catch (SecurityException e) {
System.err.println("Error: Insufficient permissions to delete directory: " + path);
e.printStackTrace();
return false;
} catch (Exception e) {
System.err.println("Error deleting directory: " + path + ". " + e.getMessage());
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
// Example usage
String dirPath = "test_dir";
//Create Directory
if (createDirectory(dirPath)) {
System.out.println("Directory created.");
} else {
System.out.println("Directory creation failed.");
}
//Check if directory exists
if (directoryExists(dirPath)) {
System.out.println("Directory exists.");
} else {
System.out.println("Directory does not exist.");
}
//Delete Directory
if (deleteDirectory(dirPath)) {
System.out.println("Directory deleted.");
} else {
System.out.println("Directory deletion failed.");
}
}
}
Add your comment