1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. public class BinaryFileTeardown {
  6. /**
  7. * Deletes binary files from a staging environment.
  8. *
  9. * @param directoryPath The path to the directory containing the binary files.
  10. * @return true if the teardown was successful, false otherwise.
  11. * @throws IOException If an I/O error occurs during file deletion.
  12. */
  13. public static boolean teardownBinaryFiles(String directoryPath) throws IOException {
  14. Path directory = Path.parse(directoryPath);
  15. if (!Files.exists(directory)) {
  16. System.err.println("Directory does not exist: " + directoryPath);
  17. return false;
  18. }
  19. File directoryFile = directory.toFile();
  20. if (!directoryFile.isDirectory()) {
  21. System.err.println("Path is not a directory: " + directoryPath);
  22. return false;
  23. }
  24. File[] files = directoryFile.listFiles();
  25. if (files == null) {
  26. System.err.println("Failed to list files in directory: " + directoryPath);
  27. return false;
  28. }
  29. for (File file : files) {
  30. if (file.isFile()) { // Only process files, not subdirectories
  31. try {
  32. Files.delete(file.toPath()); // Delete the file
  33. System.out.println("Deleted: " + file.getAbsolutePath());
  34. } catch (IOException e) {
  35. System.err.println("Error deleting file " + file.getAbsolutePath() + ": " + e.getMessage());
  36. return false; // Indicate failure if deletion fails
  37. }
  38. }
  39. }
  40. return true; // Indicate success
  41. }
  42. public static void main(String[] args) {
  43. // Example usage: Replace with your staging environment directory.
  44. String stagingDirectory = "/path/to/your/staging/directory"; //Replace with your directory
  45. try {
  46. boolean success = teardownBinaryFiles(stagingDirectory);
  47. if (success) {
  48. System.out.println("Binary file teardown completed successfully.");
  49. } else {
  50. System.err.println("Binary file teardown failed.");
  51. }
  52. } catch (IOException e) {
  53. System.err.println("An error occurred: " + e.getMessage());
  54. }
  55. }
  56. }

Add your comment