Create StringReader from a string and read char by char in Java

Description

The following code shows how to create StringReader from a string and read char by char.

Example


//  w  w  w. j  a v a  2 s .  co m
import java.io.IOException;
import java.io.StringReader;

public class Main {
  public static void main(String[] args) throws IOException {


    StringReader in2 = new StringReader("a bc ddd");
    int c;
    while ((c = in2.read()) != -1)
      System.out.print((char) c);

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip