|
| 1 | +# Module 8: I/O & Serialization |
| 2 | + |
| 3 | +## 📋 Overview |
| 4 | +Master Java I/O operations and serialization techniques essential for data handling in Spring applications. |
| 5 | + |
| 6 | +## 🎯 Learning Objectives |
| 7 | +- Understand different I/O streams and operations |
| 8 | +- Master file handling and NIO.2 |
| 9 | +- Apply serialization and deserialization techniques |
| 10 | +- Handle JSON processing effectively in Spring |
| 11 | + |
| 12 | +## 📚 I/O Fundamentals |
| 13 | + |
| 14 | +### File Operations with NIO.2 |
| 15 | +```java |
| 16 | +@Service |
| 17 | +public class FileService { |
| 18 | + |
| 19 | + public void demonstrateFileOperations() throws IOException { |
| 20 | + Path filePath = Paths.get("data", "users.txt"); |
| 21 | + |
| 22 | + // Create directories if they don't exist |
| 23 | + Files.createDirectories(filePath.getParent()); |
| 24 | + |
| 25 | + // Write to file |
| 26 | + List<String> lines = Arrays.asList("John,john@example.com", "Jane,jane@example.com"); |
| 27 | + Files.write(filePath, lines, StandardCharsets.UTF_8); |
| 28 | + |
| 29 | + // Read from file |
| 30 | + List<String> readLines = Files.readAllLines(filePath, StandardCharsets.UTF_8); |
| 31 | + |
| 32 | + // Stream file lines |
| 33 | + try (Stream<String> stream = Files.lines(filePath)) { |
| 34 | + List<User> users = stream |
| 35 | + .map(line -> line.split(",")) |
| 36 | + .map(parts -> new User(parts[0], parts[1])) |
| 37 | + .collect(Collectors.toList()); |
| 38 | + } |
| 39 | + |
| 40 | + // Copy file |
| 41 | + Path backup = Paths.get("data", "users_backup.txt"); |
| 42 | + Files.copy(filePath, backup, StandardCopyOption.REPLACE_EXISTING); |
| 43 | + |
| 44 | + // Delete file |
| 45 | + Files.deleteIfExists(backup); |
| 46 | + } |
| 47 | + |
| 48 | + public void processLargeFile(Path filePath) throws IOException { |
| 49 | + // For large files, use buffered reading |
| 50 | + try (BufferedReader reader = Files.newBufferedReader(filePath)) { |
| 51 | + String line; |
| 52 | + while ((line = reader.readLine()) != null) { |
| 53 | + processLine(line); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void processLine(String line) { |
| 59 | + // Process individual line |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### JSON Processing |
| 65 | +```java |
| 66 | +@Service |
| 67 | +public class JsonProcessingService { |
| 68 | + private final ObjectMapper objectMapper; |
| 69 | + |
| 70 | + public JsonProcessingService(ObjectMapper objectMapper) { |
| 71 | + this.objectMapper = objectMapper; |
| 72 | + } |
| 73 | + |
| 74 | + // Serialize object to JSON |
| 75 | + public String toJson(Object object) throws JsonProcessingException { |
| 76 | + return objectMapper.writeValueAsString(object); |
| 77 | + } |
| 78 | + |
| 79 | + // Deserialize JSON to object |
| 80 | + public <T> T fromJson(String json, Class<T> clazz) throws JsonProcessingException { |
| 81 | + return objectMapper.readValue(json, clazz); |
| 82 | + } |
| 83 | + |
| 84 | + // Handle generic types |
| 85 | + public <T> T fromJson(String json, TypeReference<T> typeRef) throws JsonProcessingException { |
| 86 | + return objectMapper.readValue(json, typeRef); |
| 87 | + } |
| 88 | + |
| 89 | + // Stream processing for large JSON arrays |
| 90 | + public void processLargeJsonArray(InputStream inputStream) throws IOException { |
| 91 | + try (JsonParser parser = objectMapper.getFactory().createParser(inputStream)) { |
| 92 | + if (parser.nextToken() == JsonToken.START_ARRAY) { |
| 93 | + while (parser.nextToken() == JsonToken.START_OBJECT) { |
| 94 | + User user = objectMapper.readValue(parser, User.class); |
| 95 | + processUser(user); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void processUser(User user) { |
| 102 | + // Process individual user |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## 🌸 Spring I/O Integration |
| 108 | + |
| 109 | +### Resource Handling |
| 110 | +```java |
| 111 | +@Service |
| 112 | +public class ResourceService { |
| 113 | + |
| 114 | + @Value("classpath:config/application.properties") |
| 115 | + private Resource configResource; |
| 116 | + |
| 117 | + public Properties loadProperties() throws IOException { |
| 118 | + Properties props = new Properties(); |
| 119 | + try (InputStream is = configResource.getInputStream()) { |
| 120 | + props.load(is); |
| 121 | + } |
| 122 | + return props; |
| 123 | + } |
| 124 | + |
| 125 | + public void processFile(@Value("${file.path}") String filePath) throws IOException { |
| 126 | + Resource resource = new FileSystemResource(filePath); |
| 127 | + |
| 128 | + if (resource.exists() && resource.isReadable()) { |
| 129 | + try (InputStream is = resource.getInputStream()) { |
| 130 | + // Process file |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | +**Next Module**: [Design Patterns](../module9-patterns/README.md) |
0 commit comments