import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class BinaryFileTeardown {
/**
* Deletes binary files from a staging environment.
*
* @param directoryPath The path to the directory containing the binary files.
* @return true if the teardown was successful, false otherwise.
* @throws IOException If an I/O error occurs during file deletion.
*/
public static boolean teardownBinaryFiles(String directoryPath) throws IOException {
Path directory = Path.parse(directoryPath);
if (!Files.exists(directory)) {
System.err.println("Directory does not exist: " + directoryPath);
return false;
}
File directoryFile = directory.toFile();
if (!directoryFile.isDirectory()) {
System.err.println("Path is not a directory: " + directoryPath);
return false;
}
File[] files = directoryFile.listFiles();
if (files == null) {
System.err.println("Failed to list files in directory: " + directoryPath);
return false;
}
for (File file : files) {
if (file.isFile()) { // Only process files, not subdirectories
try {
Files.delete(file.toPath()); // Delete the file
System.out.println("Deleted: " + file.getAbsolutePath());
} catch (IOException e) {
System.err.println("Error deleting file " + file.getAbsolutePath() + ": " + e.getMessage());
return false; // Indicate failure if deletion fails
}
}
}
return true; // Indicate success
}
public static void main(String[] args) {
// Example usage: Replace with your staging environment directory.
String stagingDirectory = "/path/to/your/staging/directory"; //Replace with your directory
try {
boolean success = teardownBinaryFiles(stagingDirectory);
if (success) {
System.out.println("Binary file teardown completed successfully.");
} else {
System.err.println("Binary file teardown failed.");
}
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
Add your comment