1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class FormDataImporter {
  7. private String configFilePath;
  8. private String dataFilePath;
  9. public FormDataImporter(String configFilePath, String dataFilePath) {
  10. this.configFilePath = configFilePath;
  11. this.dataFilePath = dataFilePath;
  12. }
  13. public Map<String, List<String>> importFormData() throws IOException {
  14. Map<String, List<String>> formData = new HashMap<>();
  15. // Read configuration file
  16. try (BufferedReader reader = new BufferedReader(new FileReader(configFilePath))) {
  17. String line;
  18. while ((line = reader.readLine()) != null) {
  19. String[] parts = line.split(","); // Assuming comma-separated format
  20. if (parts.length == 2) {
  21. String formField = parts[0].trim();
  22. String label = parts[1].trim();
  23. formData.put(formField, new ArrayList<>()); // Initialize list for each form field
  24. }
  25. }
  26. }
  27. // Read data file
  28. try (BufferedReader reader = new BufferedReader(new FileReader(dataFilePath))) {
  29. String line;
  30. while ((line = reader.readLine()) != null) {
  31. String[] parts = line.split(","); // Assuming comma-separated format
  32. if (parts.length > 0) {
  33. for (int i = 0; i < parts.length; i++) {
  34. String value = parts[i].trim();
  35. if(formData.containsKey(parts[0].trim())){
  36. formData.get(parts[0].trim()).add(value);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. return formData;
  43. }
  44. public static void main(String[] args) throws IOException {
  45. // Example usage
  46. String configFile = "config.txt"; // Replace with your config file path
  47. String dataFile = "data.txt"; // Replace with your data file path
  48. FormDataImporter importer = new FormDataImporter(configFile, dataFile);
  49. Map<String, List<String>> formData = importer.importFormData();
  50. // Print the imported data (for verification)
  51. for (Map.Entry<String, List<String>> entry : formData.entrySet()) {
  52. System.out.println(entry.getKey() + ": " + entry.getValue());
  53. }
  54. }
  55. }

Add your comment