Java Reader.read(CharBuffer target)

Syntax

Reader.read(CharBuffer target) has the following syntax.

public int read(CharBuffer target)  throws IOException

Example

In the following code shows how to use Reader.read(CharBuffer target) method.


/*from   ww w  .  jav  a  2  s .co  m*/
import java.io.*;
import java.nio.CharBuffer;

public class Main {

   public static void main(String[] args) {

      String s = "tutorial from java2s.com";

      CharBuffer cb = CharBuffer.allocate(100);

      Reader reader = new StringReader(s);

      try {
         reader.read(cb);

         cb.flip();

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

         reader.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.