Java - Reader read(char[] cbuf) method



Description

The Java Reader read(char[] cbuf) method 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.

Declaration

Following is the declaration for java.io.Reader.read(char[] cbuf) method.

 public int read(char[] cbuf) 

Parameters

cbuf − Destination buffer.

Return Value

This method returns the number of characters read, or -1 if the end of the stream has been reached.

Exception

  • IOException − if some I/O error occurs.

Example - Usage of Reader read(char[] cbuf) method

The following example shows the usage of Reader read(char[] cbuf) method.

ReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class ReaderDemo { public static void main(String[] args) { String s = "Hello world"; // create a StringReader Reader reader = new StringReader(s); // create a char array to read chars into char cbuf[] = new char[5]; try { // read characters into an array. System.out.println( reader.read(cbuf)); // print cbuf System.out.println(cbuf); // close the stream reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 5 Hello 

Example - Read a string in chunks using StringReader

The following example shows the usage of Reader read(char[] cbuf) method.

ReaderDemo.java

 package com.tutorialspoint; import java.io.StringReader; import java.io.IOException; public class ReaderDemo { public static void main(String[] args) { String data = "Java IO Reader Example"; try (StringReader reader = new StringReader(data)) { char[] buffer = new char[10]; // Read in chunks of 10 characters int charsRead; while ((charsRead = reader.read(buffer)) != -1) { System.out.println("Read " + charsRead + " chars: " + new String(buffer, 0, charsRead)); } } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Read 10 chars: Java IO Re Read 10 chars: ader Examp Read 3 chars: le 

Explanation

  • We use a buffer of size 10 and read repeatedly until the end of the string.

  • read(char[]) fills the buffer and returns how many characters were read.

  • We convert only the valid portion (0 to charsRead) of the buffer to a string.

Example - Use read(char[]) with a smaller buffer

The following example shows the usage of Reader read(char[] cbuf) method.

ReaderDemo.java

 package com.tutorialspoint; import java.io.StringReader; import java.io.IOException; public class ReaderDemo { public static void main(String[] args) { String data = "ABCD"; try (StringReader reader = new StringReader(data)) { char[] buffer = new char[2]; // Small buffer int charsRead; while ((charsRead = reader.read(buffer)) != -1) { System.out.println("Read: " + new String(buffer, 0, charsRead)); } } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Read: AB Read: CD 

Explanation

  • The string "ABCD" is read in 2-character chunks.

  • On each iteration, we print the part of the buffer that was filled.

java_io_reader.htm
Advertisements