import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
public class CLIArgumentSerializer {
public static void serializeArguments(Map<String, String> arguments, String filePath) throws IOException {
// Create an ObjectOutputStream to write the arguments to a file.
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
// Write the arguments map to the output stream.
oos.writeObject(arguments);
}
}
public static Map<String, String> deserializeArguments(String filePath) throws IOException, ClassNotFoundException {
// Read the arguments map from the file.
Map<String, String> arguments = new HashMap<>();
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
// Read the arguments map from the input stream.
arguments = (Map<String, String>) ois.readObject();
}
return arguments;
}
public static void main(String[] args) {
// Example Usage:
Map<String, String> arguments = new HashMap<>();
arguments.put("name", "John Doe");
arguments.put("age", "30");
arguments.put("city", "New York");
String filePath = "cli_arguments.ser";
// Serialize the arguments to a file.
try {
serializeArguments(arguments, filePath);
System.out.println("Arguments serialized to " + filePath);
// Deserialize the arguments from the file.
Map<String, String> deserializedArguments = deserializeArguments(filePath);
System.out.println("Arguments deserialized: " + deserializedArguments);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Add your comment