Java - Reader read(CharBuffer target) method



Description

The Java Reader read(CharBuffer target) method attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed.

Declaration

Following is the declaration for java.io.Reader.read(CharBuffer target) method.

public int read(CharBuffer target)

Parameters

target − The buffer to read characters into.

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.

  • NullPointerException − If target is null.

  • ReadOnlyBufferException − If target is a read only buffer.

Example - Usage of Reader read(CharBuffer target) method

The following example shows the usage of Reader read(CharBuffer target) method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.CharBuffer;

public class ReaderDemo {
   public static void main(String[] args) {
      String s = "Hello world";

      // create a new Char Buffer with capacity of 12
      CharBuffer cb = CharBuffer.allocate(12);

      // create a StringReader
      Reader reader = new StringReader(s);

      try {
         // read characters into a char buffer
         reader.read(cb);

         // flip the char buffer
         cb.flip();

         // print the char buffer
         System.out.println(cb.toString());

         // 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 −

Hello world

Example - Reading characters from a StringReader into a CharBuffer

The following example shows the usage of Reader read(CharBuffer target) method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.StringReader;
import java.io.IOException;
import java.nio.CharBuffer;

public class ReaderDemo {
   public static void main(String[] args) {
      String data = "HelloWorld";
      try (StringReader reader = new StringReader(data)) {
         CharBuffer buffer = CharBuffer.allocate(5); // Can hold 5 characters

         int charsRead = reader.read(buffer); // Read into CharBuffer
         buffer.flip(); // Prepare for reading

         System.out.println("Characters read: " + charsRead);
         System.out.println("Buffer content: " + buffer.toString());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Characters read: 5
Buffer content: Hello

Explanation

  • A CharBuffer with capacity 5 is allocated.

  • reader.read(buffer) fills the buffer from the string "HelloWorld".

  • buffer.flip() switches it from write mode to read mode so we can access the content.

  • Only 5 characters (Hello) are read into the buffer.

Example - Reading in multiple chunks using CharBuffer

The following example shows the usage of Reader read(CharBuffer target) method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.StringReader;
import java.io.IOException;
import java.nio.CharBuffer;

public class ReaderDemo {
   public static void main(String[] args) {
      String data = "JavaCharBufferExample";
      try (StringReader reader = new StringReader(data)) {
         CharBuffer buffer = CharBuffer.allocate(6);
         int charsRead;

         while ((charsRead = reader.read(buffer)) != -1) {
            buffer.flip(); // switch to read mode
            System.out.println("Read " + charsRead + " chars: " + buffer.toString());
            buffer.clear(); // clear buffer for next read
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read 6 chars: JavaCh
Read 6 chars: arBuff
Read 6 chars: erExam
Read 3 chars: ple

Explanation

  • A loop reads the input string in chunks of 6 characters into a CharBuffer.

  • flip() prepares the buffer for reading, and clear() resets it for the next write.

  • Useful for processing large text in smaller parts.

java_io_reader.htm
Advertisements