1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.ObjectOutputStream;
  4. import java.util.List;
  5. public class LogFileSerializer {
  6. /**
  7. * Serializes a list of log file objects to a file.
  8. * @param logFiles The list of log file objects to serialize.
  9. * @param filePath The path to the file where the serialized data will be written.
  10. * @param retryIntervalMillis The retry interval in milliseconds for dry-run scenarios.
  11. * @throws IOException If an I/O error occurs during serialization.
  12. */
  13. public static void serializeLogFiles(List<LogFile> logFiles, String filePath, long retryIntervalMillis) throws IOException {
  14. // Create an ObjectOutputStream to write the serialized data to the file.
  15. try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
  16. // Serialize the list of log files.
  17. oos.writeObject(logFiles);
  18. System.out.println("Log files serialized to: " + filePath);
  19. }
  20. }
  21. /**
  22. * Deserializes a list of log file objects from a file.
  23. * @param filePath The path to the file containing the serialized data.
  24. * @return The list of log file objects.
  25. * @throws IOException If an I/O error occurs during deserialization.
  26. */
  27. public static List<LogFile> deserializeLogFiles(String filePath) throws IOException {
  28. List<LogFile> logFiles = null;
  29. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
  30. logFiles = (List<LogFile>) ois.readObject();
  31. System.out.println("Log files deserialized from: " + filePath);
  32. } catch (ClassNotFoundException e) {
  33. System.err.println("Class not found: " + e.getMessage());
  34. }
  35. return logFiles;
  36. }
  37. // Define a simple LogFile class for demonstration purposes.
  38. public static class LogFile implements Serializable {
  39. private String fileName;
  40. private String content;
  41. public LogFile(String fileName, String content) {
  42. this.fileName = fileName;
  43. this.content = content;
  44. }
  45. public String getFileName() {
  46. return fileName;
  47. }
  48. public String getContent() {
  49. return content;
  50. }
  51. @Override
  52. public String toString() {
  53. return "LogFile{" +
  54. "fileName='" + fileName + '\'' +
  55. ", content='" + content + '\'' +
  56. '}';
  57. }
  58. }
  59. public static void main(String[] args) throws IOException {
  60. // Example usage:
  61. List<LogFile> logFiles = List.of(
  62. new LogFile("app.log", "Application started"),
  63. new LogFile("error.log", "An error occurred")
  64. );
  65. String filePath = "log_files.ser";
  66. long retryInterval = 1000; // 1 second retry interval
  67. serializeLogFiles(logFiles, filePath, retryInterval);
  68. // Deserialize the log files.
  69. List<LogFile> deserializedLogFiles = deserializeLogFiles(filePath);
  70. System.out.println("Deserialized Log Files: " + deserializedLogFiles);
  71. }
  72. }

Add your comment