Java - Reader read() method



Description

The Java Reader read() method reads a single character. This method will block until a character 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() method.

public int read()

Parameters

NA

Return Value

This method returns the character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.

Exception

  • IOException − if some I/O error occurs.

Example - Usage of Reader read() method

The following example shows the usage of Reader read() 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 new StringReader
      Reader reader = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print( c);
         }

         // change line
         System.out.println();

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

Example - Reading One Character at a Time Using StringReader

The following example shows the usage of Reader read() 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 = "ABC";
      try (StringReader reader = new StringReader(data)) {
         int ch;
         while ((ch = reader.read()) != -1) {
            System.out.println("Read character: " + (char) ch);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read character: A
Read character: B
Read character: C

Explanation

  • We read one character at a time using read().

  • The loop continues until read() returns -1, which signals the end of the stream.

  • Each character is printed after casting from int to char.

Example - Reading into a char[] Buffer Using read(char[] cbuf)

The following example shows the usage of Reader read() 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 = "Hello, world!";
      try (StringReader reader = new StringReader(data)) {
         char[] buffer = new char[5];
         int charsRead;

         while ((charsRead = reader.read(buffer)) != -1) {
            System.out.println("Read " + charsRead + " characters: " + 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 5 characters: Hello
Read 5 characters: , wor
Read 3 characters: ld!

Explanation

  • read(char[] cbuf) reads as many characters as will fit into the buffer.

  • It returns how many characters were actually read.

  • We print the contents of the buffer after each read.

java_io_reader.htm
Advertisements