import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class DirectoryLogger {
/**
* Logs directory operations with optional flags.
* @param directoryPath The path to the directory to log.
* @param operation The operation performed (e.g., "create", "delete", "list").
* @param flag1 Optional first flag.
* @param flag2 Optional second flag.
* @param flag3 Optional third flag.
*/
public static void logDirectoryOperation(String directoryPath, String operation, String flag1, String flag2, String flag3) {
// Validate inputs
if (directoryPath == null || directoryPath.isEmpty()) {
System.err.println("Error: Directory path cannot be null or empty.");
return;
}
if (operation == null || operation.isEmpty()) {
System.err.println("Error: Operation cannot be null or empty.");
return;
}
File directory = new File(directoryPath);
// Log the operation
System.out.println("Operation: " + operation + " on directory: " + directoryPath);
if (flag1 != null && !flag1.isEmpty()) {
System.out.println(" Flag 1: " + flag1);
}
if (flag2 != null && !flag2.isEmpty()) {
System.out.println(" Flag 2: " + flag2);
}
if (flag3 != null && !flag3.isEmpty()) {
System.out.println(" Flag 3: " + flag3);
}
// Perform the directory operation (example)
try {
if (operation.equalsIgnoreCase("create")) {
if (!directory.exists()) {
directory.mkdirs(); // Create the directory if it doesn't exist
System.out.println(" Directory created successfully.");
} else {
System.out.println(" Directory already exists.");
}
} else if (operation.equalsIgnoreCase("delete")) {
if (directory.delete()) {
System.out.println(" Directory deleted successfully.");
} else {
System.err.println(" Failed to delete directory.");
}
} else if (operation.equalsIgnoreCase("list")) {
List<String> files = new ArrayList<>();
File[] filesArray = directory.listFiles();
if (filesArray != null) {
for (File file : filesArray) {
files.add(file.getName());
}
}
System.out.println(" Files in directory: " + files);
} else {
System.err.println("Error: Invalid operation specified.");
}
} catch (Exception e) {
System.err.println("Error during directory operation: " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
// Example usage
logDirectoryOperation("/path/to/my/directory", "create", "verbose", "timestamp", "user");
logDirectoryOperation("/path/to/my/directory", "list", null, null, null);
logDirectoryOperation("/path/to/my/directory", "delete", "force", null, null);
}
}
Add your comment