CodeGym /Courses /Java Core /Reader/Writer

Reader/Writer

Java Core
Level 9 , Lesson 2
Available

"Hello, Amigo! Today, Ellie told you about the adapter pattern."

Most classes related to I/O streams are implemented as adapters. They either convert equivalent interfaces or they connect them, starting from the simple and moving up to the complex.

"Are InputStreamReader and BufferedReader also adapters? At the very least, they are very similar to adapters in the way they are used: After an object is created, it is passed to the constructor of another class."

"Yes, InputStreamReader converts the InputStream interface to the Reader interface. BufferedReader is not an adapter in its purest form, because Java's creators decided not to give its methods their own separate interface. But it is kindred spirit."

Instead of writing a bazillion different classes, Java's creators wrote two dozen adapters and allowed them to connect to each other however a programmer might want.

This approach is very convenient. A programmer can always write her class and/or adapter, have it implement a standard interface, and include it in the chain of adapter objects she is building.

"So that's how it all works. Instead of big complex classes, we make chains of simple objects and adapters. And then you just create them and combine them in the right order!"

"And you implement whatever is missing."

"Yes, I get it."

"But actually I wanted to tell you about Reader and Writer today. These are two abstract classes that are very similar to the InputStream and OutputStream classes. But unlike those classes, these two classes work with characters. They read and write characters. They are very convenient when working with textual information. Let's take a look at the methods they have:"

Reader methods What the method does
int read(char[] cbuf);
"This method immediately reads several characters into the buffer (char array), until the buffer is full or until the source doesn't have any more characters to read."
The method returns the number of characters actually read (which can be less than the length of the array)
int read();
"This method reads one character and returns it. The result is widened to an int for looks. If there are no available characters, then the method returns -1."
boolean ready();
This method returns true if there are any unread characters for the read methods
void close();
This method «closes» the stream. You call this when you're done working with the stream.
The object then performs the housekeeping operations needed to close the file, etc.
At this point, you can't read any more data from the stream.

"It turns out that Reader's read(char [] cbuf) method lets us read whole blocks of characters, rather than one character at a time. So it's faster and more convenient."

"Exactly. And now let's see what methods Writer has:"

Method What the method does
void write(int c);
This method writes one character. The int type is narrowed to a char. The extra part is simply discarded.
void write(char[] cbuff);
This method writes an array of characters.
void write(String s);
This method writes a string. It is simply converted into an array of characters and then the second method is called.
void flush();
If the stream is internally storing any data that has not yet been written, this method forces it to be written.
void close();
This method «closes» the stream. You call this when you're done working with the stream.
The object then performs the housekeeping operations needed to close the file, etc. You can't write data to the stream any longer, and flush is called automatically.

It's important to understand that Reader and Writer are abstract classes. They don't do anything and contain virtually no code. All their methods will need to be implemented in the classes that inherit them. Their job is to standardize how classes interact. Developers don't need to invent their own standards to interact with each other. It is much more convenient for everyone to maintain a few basic standards. This allows classes written by different programmers to easily interact not only with classes written by Java's creators, but also with classes written by other programmers.

Standards are powerful.

"I agree. Supporting common standards is beneficial to everyone."

Comments (12)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Joshua Level 19, DALLAS, Republic of texas
30 June 2022
In gof design patterns, they says a adapter is also known as a wrapper. That statement alone cleared up a lot of questions I had....
Aldo Luna Bueno Level 1, Peru
26 March 2022
Instead of writing a bazillion different classes, Java's creators wrote two dozen adapters and allowed them to connect to each other however a programmer might want. I don't understand this. How come adapters simplify the software infrastructure so much? I would understand if you were referring to the decorator design pattern, as it allows you to create objects with a flexibility that could only be matched by creating many classes, but I don't get it with the adapter pattern.
Tom Level 41, San Jose, Sweden
23 February 2021
😀
Collin M Level 22, Germiston, South Africa
3 October 2020
I Understand how to use the Java's I/O classes, but so far I can't implement Adapter Design Pattern.
Andrei Level 41
2 February 2021
No problem, my friend, you know the saying, practice makes perfect, Rome wasn't build in one day, etc, etc you get the point. 😁
Collin M Level 22, Germiston, South Africa
11 February 2021
That's definitely true my friend 😀
Robert Constantinescu Level 25, Bucharest, Romania
17 November 2019
this level is lit. The info is becoming more and more challenging. I like this !
Denske Vselenske Level 20, Saint Petersburg, Russian Federation
1 August 2019
dzopa
Ewerton Level 30, Belo Horizonte, Brasil
4 July 2019
Sorry, I disagree, just don't know why.
Profj Level 20, Lagos, Nigeria
29 June 2019
I agree too!!!. Who else is agreeing?
Satya Vath Level 31, Vijayawada, India
19 April 2019
I agree tooooo!!!!!
Hashirama Level 26, Port-Harcourt, Nigeria
23 February 2019
I agree too!!!