We can create streams of data from files, network resources, memory locations, etc, both input and output. To initially demonstrate the use of streams, we'll use streams around a file, both byte and Character streams. The methods introduced in these example can be used for any stream.
InputStreams and OutputStreams are streams of bytes:
Readers and Writers are streams of Characters:
When we write to an output stream or writer such as FileWriter, we should always close() it in some way:
We can choose from many such methods to read and write characters to streams, where the stream is always closed automatically. Here's some methods which use a Reader and/or Writer. Although these examples use Files, all these methods work for other streamed resources also.
Some methods which use an input and/or output stream which, although using Files in the examples, all work for other streamed resources also:
Although the examples in this tutorial used streams, readers, and writers around files, they're all available for streams, readers, and writers around all other resources also. Our next tutorial will introduce resource-specific streams.