Java Streams  «Prev  Next»

Lesson 7

Java Streams Conclusion

This module introduced you to streams which are the foundation of input and output in Java. In this module, you learned what streams are, where streams come from, and when you should use streams.
A Java program can read from multiple data sources and destinations, like a file on your storage device, another application, an array in memory, a network connection, and others. Figure 2.6 from the previous page shows different I/O streams, input streams and output streams and how they are connected to data sources and destinations.

Understanding variations of Data

Java I/O uses separate classes to read and write different types of data. The data is broadly divided into byte data and character data. The byte streams read and write byte data. To read and write characters and text data, Java I/O uses readers and writers, referred to as character streams. Byte streams can be used to read and write practically all kinds of data including zip files, image or video files, text or PDF files, and others. Character streams are used to read and write character data. Figure 2.7 shows these streams.
Main categories of Java I/O streams: byte streams and character streams
Figure 2.7 Main categories of Java I/O streams: byte streams and character streams


Read | Write byte Data

The objective of streams is to create files and read or write byte data from them or to them. Even though you can read and write practically all byte data by using byte streams, you might need the assistance of specific classes to interpret the read data or written data in a particular format. For example, to create a PDF file, interpret its data, and write data to it, you might need to use an API by Adobe or a third party.
Going back to the basics, a computer system reads and writes all data as binary data (0s and 1s, referred to as bits) from all sources and to all destinations. A byte stream reads and write bytes (8 bits) to and from data sources. All the other I/O classes build on the byte streams, adding more functionality. For example, character streams take into account Unicode characters and the user's charset. Instead of reading or writing 8-bit data from or to a file, character streams define methods to read or write 16-bit data. But behind the scenes they use byte streams to get their work done.
In the next module you will learn about efficiently reading data from input streams. In addition, you will learn how to use the available() method to count available bytes in a stream, how to skip unnecessary data, and when to close streams.