1. import java.util.*;
  2. import java.io.*;
  3. public class CollectionRestorer {
  4. /**
  5. * Restores data of collections from a file.
  6. * Handles potential file reading errors and data parsing issues gracefully.
  7. * @param filename The name of the file containing the collection data.
  8. * @param <T> The type of elements in the collections.
  9. * @return A map containing the restored collections, or null if an error occurred.
  10. */
  11. public <T> Map<String, List<T>> restoreCollections(String filename) {
  12. Map<String, List<T>> restoredData = new HashMap<>();
  13. try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
  14. String line;
  15. while ((line = reader.readLine()) != null) {
  16. line = line.trim(); // Remove leading/trailing whitespace
  17. if (line.isEmpty()) {
  18. continue; // Skip empty lines
  19. }
  20. // Parse the line to extract collection name and data
  21. String[] parts = line.split(":", 2); // Split into name and data
  22. if (parts.length != 2) {
  23. System.err.println("Invalid line format: " + line); //Log error
  24. continue; // Skip invalid lines
  25. }
  26. String collectionName = parts[0].trim();
  27. String dataString = parts[1].trim();
  28. List<T> collection = parseCollectionData(dataString);
  29. if (collection != null) {
  30. restoredData.put(collectionName, collection);
  31. } else {
  32. System.err.println("Failed to parse collection data for: " + collectionName); //Log error
  33. }
  34. }
  35. } catch (IOException e) {
  36. System.err.println("Error reading file: " + e.getMessage()); //Log error
  37. return null;
  38. }
  39. return restoredData;
  40. }
  41. /**
  42. * Parses a string containing collection data into a List of objects.
  43. * This is a placeholder and can be customized based on the specific data format.
  44. * @param dataString The string containing the collection data.
  45. * @param <T> The type of elements in the collections.
  46. * @return A list of objects, or null if parsing fails.
  47. */
  48. private <T> List<T> parseCollectionData(String dataString) {
  49. // Placeholder: Replace with your specific parsing logic
  50. // This example assumes comma-separated values
  51. if (dataString.isEmpty()) {
  52. return new ArrayList<>(); // Return empty list if data is empty
  53. }
  54. String[] values = dataString.split(",");
  55. List<T> collection = new ArrayList<>();
  56. for (String value : values) {
  57. try {
  58. //Attempt to parse as Integer, if fails, try Double, otherwise String
  59. try{
  60. collection.add((T)Integer.parseInt(value.trim()));
  61. } catch(NumberFormatException e){
  62. try{
  63. collection.add((T)Double.parseDouble(value.trim()));
  64. } catch(NumberFormatException e2) {
  65. collection.add((T)value.trim());
  66. }
  67. }
  68. } catch (Exception e) {
  69. System.err.println("Error parsing value: " + value); //Log error
  70. return null; // Return null if parsing fails
  71. }
  72. }
  73. return collection;
  74. }
  75. public static void main(String[] args) {
  76. CollectionRestorer restorer = new CollectionRestorer();
  77. Map<String, List<Integer>> restoredCollections = restorer.restoreCollections("data.txt");
  78. if (restoredCollections != null) {
  79. for (Map.Entry<String, List<Integer>> entry : restoredCollections.entrySet()) {
  80. System.out.println("Collection: " + entry.getKey() + ", Data: " + entry.getValue());
  81. }
  82. } else {
  83. System.err.println("Failed to restore collections.");
  84. }
  85. }
  86. }

Add your comment