Java I/O Streams overview
1. Java I/O Streams overview
Welcome back! Building on our file handling basics, we'll explore Java I/O streams for efficient data processing.2. Stream fundamentals
Streams are sequences of data flowing between a source and a destination. They're unidirectional and handle data in a First-In, First-Out order. Streams work like pipes, with data flowing in the pipes! Java provides two main categories: byte streams for binary data using InputStream/OutputStream, and character streams for text using Reader/Writer. Character streams automatically handle text encoding, crucial for international data.3. Byte Streams
Byte streams process binary data like images and audio. As we just mentioned, there's an InputStream for reading operations and an OutputStream for writing operations. When working with files, we'll use FileInputStream or FileOutputStream. Key methods include read(), which retrieves bytes, and write(), which outputs them.4. Byte Streams
In this example, FileInputStream sits inside a try-with-resources statement, which automatically closes the stream when done. We create fis to read from data.bin, then create a buffer array to hold our data and bytesRead to track how many bytes we've read. We loop through the data reading 1024-byte chunks using fis.read(buffer), which returns bytes read or -1 at file end. Then print the byte count. This buffered approach is more efficient than reading single bytes.5. Character Streams
For text data like CSV, character streams are ideal. Reader and Writer, and their FileReader/FileWriter and BufferedReader/BufferedWriter implementations, offer text-specific conveniences like readLine(), which captures entire lines at once. These are useful for structured text data, such as CSV files.6. Character Streams
This example wraps BufferedReader around FileReader. readLine() provides each line as a complete String, perfect for line-based formats like CSV. Character streams simplify text processing and are more common in data import tasks.7. Buffered Streams - performance
Buffering improves performance by reducing disk operations. BufferedInputStream and BufferedOutputStream add memory buffers to byte streams, reading and writing larger chunks instead of single bytes. BufferedReader and BufferedWriter do the same for character streams while adding conveniences like readLine() for reading entire text lines. These wrapper classes boost efficiency for large files or many small read/write operations.8. Buffered Streams - performance
In this example, we read the header line separately - a common pattern for CSV files - then count the rows. With large datasets, buffering significantly improves performance. It is good practice to use buffered streams when working with data files.9. Stream chaining
Stream chaining creates powerful processing pipelines, with each individual stream adding some functionality. A common pattern is FileInputStream - InputStreamReader - BufferedReader, allowing flexible, modular data processing. We must remember to close the outermost stream!10. Stream chaining
This example chains three streams: FileInputStream reads raw bytes, InputStreamReader decodes characters, and BufferedReader provides efficient line reading. Closing the outer stream closes the inner ones too. This approach gives tremendous flexibility in data processing.11. Exception handling
Proper exception handling is crucial for I/O operations. Any exceptions should be identified and caught, two common types being IOException and FileNotFoundException, which link to our data validation checks in the last lesson. As we have in our previous code examples, use try/with blocks to catch exceptions.12. Exception handling
This example shows a reusable method that encapsulates file reading with try-with-resources for automatic cleanup. It throws IOException rather than handling it internally, letting the caller decide the response. This separation of I/O concerns from business logic improves code reusability and is essential for robust data importing.13. Let's practice!
Now let's handle some streams!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.