Reader, Writer

Java Collections
Level 1 , Lesson 7
Available

"Hi, Amigo, once again."

"Hello to you, Rishi. What will today's lesson be about?"

"Today I'm going to tell you about Reader and Writer."

"But, Rishi, I already know almost everything about them!"

"Repetition is the mother of learning."

"Hmm. Well, okay."

"Reader and Writer are analogs to InputStream and OutputStream, but they work with characters, not bytes. Sometimes they are also called character streams, as opposed to InputStream and OutputStream, which are called byte streams."

"One is for characters, the other is for bytes. I remember."

"It's not just that. These classes are specially designed to work with text and strings. Today we will look at two classic implementations of these abstract classes: FileReader and FileWriter."

"Here are the methods of the FileReader class:"

Method Description
int read() Reads one character from the stream and returns it.
int read(char cbuf[], int offset, int length) Reads an array of characters, returns the number of characters read.
boolean ready() Returns true if it is possible to read from the stream.
void close() Closes the input stream.
int read(java.nio.CharBuffer target) Read a set of characters into a buffer.
int read(char cbuf[]) Reads an array of characters.
long skip(long n) Skips n characters in the stream.
String getEncoding() Returns the current encoding of the stream.

"Well, I know almost all of this. But what about FileWriter?"

Method Description
void write(int c) Writes one character to the stream.
void write(char cbuf[], int off, int len) Writes an array of characters to the stream.
void write(char cbuf[]) Writes an array of characters to the stream.
void write(String str, int off, int len) Writes part of a string to the stream.
void write(String str) Writes a string to the stream.
void flush() Writes everything cached in memory to disk.
void close() Closes the stream.
String getEncoding() Returns the current encoding of the stream.

"I know that!"

"That's wonderful. Then let's look at one interesting example, and later Diego will give you more tasks."

"How do you read a file line by line? Can you write the code?"

"Easy, look:"

Code
// Create a list for storing the lines
List<String> list = new ArrayList<String>();

// Open the file
File file = new File("c:/document.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));

// As long as the file isn't empty, read from it
while (reader.ready())
{
 list.add(reader.readLine());
}

// Close the file
reader.close();

"Hmm. Not bad."

"Not bad? It's all simple and beautiful. Admit it, Rishi—I already have an excellent mastery of I/O threads. So what could be improved here?"

"Well, for example, you could do this:"

Rishi's code
File file = new File("c:/document.txt");

List list = Files.readAllLines(file.toPath(), Charset.defaultCharset());

"Hmm. That is shorter. And just today you told me about all these methods. I'll rework it. Thank you for the lesson, Rishi."

"Good luck, Amigo."

Comments (3)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Niko Level 41, Wuhan, China
11 April 2022
"Reader and Writer are analogs to InputStream and OutputStream, but they work with characters, not bytes. Sometimes they are also called character streams, as opposed to InputStream and OutputStream, which are called byte streams."
Ruslan Skaldin Level 33, Tashkent, Uzbekistan
30 November 2020
After previous zip task, I'm afraid what tasks waiting for us in the next lesson =)
Jen P Level 26
16 August 2019
I think the description in this post is not very accurate, for example int read(char cbuf[]) Reads an array of characters. which leads me thinking it read from the given char array; But from official java doc: "public int read(char[] cbuf) throws IOException Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached"