CodeGym /Courses /Java Core /Input/Output streams

Input/Output streams

Java Core
Level 8 , Lesson 1
Available

"Hello, Amigo! Today we'll get acquainted with input/output streams. We picked at this topic a couple of days ago, but today we will explore it thoroughly. Input/output streams are divided into 4 categories:"

1) Streams are divided according to their direction: input streams and output streams

2) Streams are divided according to their data type: those that work with bytes and those that work with characters.

Here these divisions are represented in a table:

Input stream Output stream
Works with bytes InputStream OutputStream
Works with characters Reader Writer

If an object implements the InputStream interface, then it supports the ability to sequentially read bytes from it.

If an object implements the OutputStream interface, then it supports the ability to sequentially write bytes to it.

If an object implements the Reader interface, then it supports the ability to sequentially read characters (chars) from it.

If an object implements the Writer interface, then it supports the ability to sequentially write characters (chars) to it.

Input/Output streams - 1

An output stream is like a printer. We can output documents to the printer. We can output data to an output stream.

For its part, an input stream can be compared to a scanner, or perhaps an electrical outlet. With a scanner, we can bring documents onto our computer. Or we can plug into an electrical outlet and receive electricity from it. We can receive data from an input stream.

"Where are they used?"

"These classes are used everywhere in Java. Our familiar friend System.in is a static InputStream variable named in in the System class."

"Seriously?! So all this time I've been using an InputStream and didn't even realize it. Is System.out also a stream?"

"Yes, System.out is a static PrintStream (a descendant of OutputStream) variable in the System class."

"You mean to tell me that I've always been using streams and didn't even know it?"

"Yes, and that just tells us how convenient these streams are. You just grab one and use it."

"But you can't say that about System.in. We constantly had to add BufferedReader or InputStreamReader to it."

"That's true. But there were also reasons for that."

There are a lot of data types, and many ways to work with them. So the number of standard I/O classes grew very quickly, though they did everything in almost the same way. To avoid this complexity, Java developers used the principle of abstraction and divided classes into many small parts.

But you can connect these parts in a coherent way and get very complex functionality, if you need it. Look at this example:

Output a string to the console
System.out.println("Hello");
Store the console output stream in a separate variable.
Output a string to the stream.
PrintStream console = System.out;
console.println("Hello");
Create a dynamic (expanding) byte array in memory.
Connect it to a new output stream (PrintStream object).
Output a string to the stream.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintStream console = new PrintStream(stream);
console.println("Hello");

"Honestly, this is like a Lego set. Only it's not clear to me what any of this code is doing."

"Don't worry about that for now. Everything in its own due time."

This is what I want you to remember: If a class implements the OutputStream interface, you can write bytes to it. Almost exactly like you output data to the console. What it does with it is its business. With our "Lego kit", we don't care about the purpose of each individual part. We care about the fact that the large selection of parts lets us build such cool things.

"Okay. Then where do we start?"

Comments (8)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
TheTester Level 46
14 March 2023
InputStream, Outputstream, Reader, Writer are Abstract Classes which you can extends not implements.
Aldo Luna Bueno Level 1, Peru
18 March 2022
Just as there is no point in questioning the purpose of a particular Lego piece, there is also no point in wondering what a particular class is for. What we care about is building things with those parts to meet certain goals.
ugabuga Level 36
10 October 2021
Why would you talk about InputStream and OutputStreams as interfaces? According to the java.io class hierarchy these are classes and not interfaces. https://docs.oracle.com/javase/8/docs/api/java/io/package-tree.html Can you please clarify as I'm confused when trying to structure theses concepts and classes and interfaces are used interchangeably.
Ibrahim Level 41, Sheffield, United Kingdom
20 December 2021
Please read this for further clarification: https://codegym.cc/quests/lectures/questcore.level03.lecture08
Gellert Varga Level 23, Szekesfehervar, Hungary
20 May 2021
I was confused by the name of the variable "console" in the last example. I would rather call it that:

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      PrintStream toTheByteArrayOutputStream = new PrintStream(stream);
      toTheByteArrayOutputStream.println("Hello"); // it doesn't print anything to the console!
And from the Oracle's website: "ByteArrayOutputStream= this class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString()."
Oliver Heintz Level 18, Mustang, United States
5 March 2021
I like it when Amigo says that it not clear to him. It's like, "Me too, buddy. Me too."
TomL Level 30, Prague, Czech Republic
22 February 2021
It is confusing if you write "If an object implements InputStream inferface.." when InputStream/OutputStream is an Abstract class and then in lesson 4 of this level you say it was simplistic approach. It is not simplistic, it is just confusion..
Ruslan Skaldin Level 33, Tashkent, Uzbekistan
20 June 2020
If someone interested "this is like a Lego set" like concept called Decorator Pattern. Very interesting pattern.