Reader read(char[]) method in Java with Examples

8 May 2025 | 4 min read

In Java, the provided characters are read into an array using the read(char[]) function of the Reader Class. The number of characters that were successfully read is returned after attempting to read as many as the array's length. This technique is commonly employed when working with text data streams and provides a quick and effective approach to reading several characters at once. Closing the stream after use is crucial for handling possible IOExceptions and guaranteeing appropriate resource management. This method blocks the stream up to:

  • Having received some input from the stream, this procedure stops the stream.
  • An IOException has happened.
  • As it reads, it has reached the end of the stream.

Syntax:

Parameters: The character array that will be written in the stream is the required parameter charArray that this function takes.

Return Value: The number of characters read from the stream is returned as an integer value by this method. If no character has been read, it returns -1.

Exception: If an error occurs during input-output, the above method throws an IOException.

Example 1:

To handle character streams, the code makes use of the Reader class and its read(char[]) method. Multiple characters can be read buffered in a single operation by using the read method to read data into a character array from a StringReader instance acting as the character source. The array progressively records characters, and a utility method for structured output is used to display the data. The code complies with best practices by efficiently managing resources, and the Reader instance guarantees compatibility with a variety of stream-based input sources. After the procedure, the Reader instance is closed to ensure proper resource management. This method reads several characters from a character stream at once with efficiency.

Implementation:

Output:

 [H, e, l, l, o, , W, o] 

Example 2:

In order to ensure that the entire string is captured in a single operation, the code shows how to use the Reader class and its read(char[]) method to read characters from a StringReader into a character array of dynamic length, matching the input string. The StringReader serves as a source for character data, and the read method sequentially transfers the data to the array. The program uses an array to represent structured data and displays its contents. The program effectively manages resources by closing the reader, which ensures efficient stream handling. The array is used to sequentially store the read data, which is provided by the StringReader as an in-memory character stream. The application closes the Reader after use in order to ensure proper stream handling.

Implementation:

Output:

 [H, e, l, l, o, , W, o, r, l, d] 

Example 3:

Using BufferedReader and its read() method, the code shows how to read characters from the standard input stream. To bridge byte and character streams and transform input into readable characters, it makes use of InputStreamReader. Before being processed, each character is read as an integer and converted to char. Continuous reading until the designated termination condition ('j') is satisfied is made possible via a do-while loop. The application uses BufferedReader's buffering capabilities to guarantee efficient reading.

Implementation:

Output:

 Enter the characters: , 'j' to quit. asdfghjkl a s d f g h j