Java.io.PushbackReader.read() Method

Advertisements


Description

The java.io.PushbackReader.read(char[] cbuf,int off,int len) method reads characters into a portion of an array.

Declaration

Following is the declaration for java.io.PushbackReader.read() method

public int read(char[] cbuf,int off,int len)

Parameters

  • cbuf -- Destination buffer

  • off -- Offset at which to start writing characters

  • len -- Maximum number of characters to read

Return Value

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

Exception

  • IOException -- If an I/O error occurs

Example

The following example shows the usage of java.io.PushbackReader.read() method.

package com.tutorialspoint;

import java.io.*;

public class PushbackReaderDemo {

   public static void main(String[] args) {

      String s = "Hello World";

      // create a new StringReader
      StringReader sr = new StringReader(s);

      // create a new PushBack reader based on our string reader
      PushbackReader pr = new PushbackReader(sr, 20);

      // create a char array to read chars into
      char cbuf[] = new char[5];

      try {
         // read characters into an array.
         System.out.println("" + pr.read(cbuf));

         // print cbuf
         System.out.println(cbuf);

         // Close the stream 
         pr.close();

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

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

5
Hello



Advertisements
Advertisements