1. import java.io.File;
  2. import java.io.IOException;
  3. public class DirectoryManager {
  4. /**
  5. * Creates a directory, handling potential exceptions and ensuring defensive behavior.
  6. * @param path The path to the directory to create.
  7. * @return True if the directory was created successfully, false otherwise.
  8. */
  9. public static boolean createDirectory(String path) {
  10. File dir = new File(path);
  11. // Defensive check: Check if the path is null or empty.
  12. if (path == null || path.trim().isEmpty()) {
  13. System.err.println("Error: Path cannot be null or empty.");
  14. return false;
  15. }
  16. // Defensive check: Check if the parent directory exists.
  17. File parentDir = dir.getParentFile();
  18. if (parentDir != null && !parentDir.exists()) {
  19. System.err.println("Error: Parent directory does not exist.");
  20. return false;
  21. }
  22. try {
  23. if (dir.mkdir()) {
  24. System.out.println("Directory created successfully: " + path);
  25. return true;
  26. } else {
  27. System.err.println("Error: Failed to create directory: " + path);
  28. return false;
  29. }
  30. } catch (SecurityException e) {
  31. System.err.println("Error: Insufficient permissions to create directory: " + path);
  32. e.printStackTrace();
  33. return false;
  34. } catch (Exception e) {
  35. System.err.println("Error creating directory: " + path + ". " + e.getMessage());
  36. e.printStackTrace();
  37. return false;
  38. }
  39. }
  40. /**
  41. * Checks if a directory exists, handling potential exceptions.
  42. * @param path The path to the directory.
  43. * @return True if the directory exists, false otherwise.
  44. */
  45. public static boolean directoryExists(String path) {
  46. File dir = new File(path);
  47. if (path == null || path.trim().isEmpty()) {
  48. System.err.println("Error: Path cannot be null or empty.");
  49. return false;
  50. }
  51. return dir.exists() && dir.isDirectory();
  52. }
  53. /**
  54. * Deletes a directory and its contents recursively, with defensive checks.
  55. * @param path The path to the directory to delete.
  56. * @return True if the directory was deleted successfully, false otherwise.
  57. */
  58. public static boolean deleteDirectory(String path) {
  59. File dir = new File(path);
  60. if (path == null || path.trim().isEmpty()) {
  61. System.err.println("Error: Path cannot be null or empty.");
  62. return false;
  63. }
  64. if (!dir.exists() || !dir.isDirectory()) {
  65. System.err.println("Error: Directory does not exist or is not a directory: " + path);
  66. return false;
  67. }
  68. try {
  69. if (dir.delete()) {
  70. System.out.println("Directory deleted successfully: " + path);
  71. return true;
  72. } else {
  73. System.err.println("Error: Failed to delete directory: " + path);
  74. return false;
  75. }
  76. } catch (SecurityException e) {
  77. System.err.println("Error: Insufficient permissions to delete directory: " + path);
  78. e.printStackTrace();
  79. return false;
  80. } catch (Exception e) {
  81. System.err.println("Error deleting directory: " + path + ". " + e.getMessage());
  82. e.printStackTrace();
  83. return false;
  84. }
  85. }
  86. public static void main(String[] args) {
  87. // Example usage
  88. String dirPath = "test_dir";
  89. //Create Directory
  90. if (createDirectory(dirPath)) {
  91. System.out.println("Directory created.");
  92. } else {
  93. System.out.println("Directory creation failed.");
  94. }
  95. //Check if directory exists
  96. if (directoryExists(dirPath)) {
  97. System.out.println("Directory exists.");
  98. } else {
  99. System.out.println("Directory does not exist.");
  100. }
  101. //Delete Directory
  102. if (deleteDirectory(dirPath)) {
  103. System.out.println("Directory deleted.");
  104. } else {
  105. System.out.println("Directory deletion failed.");
  106. }
  107. }
  108. }

Add your comment