Example usage for java.io StringReader StringReader

List of usage examples for java.io StringReader StringReader

Introduction

In this page you can find the example usage for java.io StringReader StringReader.

Prototype

public StringReader(String s) 

Source Link

Document

Creates a new string reader.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    int i = 0;//from  ww w  .  jav a2  s  . c  o m

    Reader r = new StringReader("ABCDEFG");

    // create new filter reader
    FilterReader fr = new FilterReader(r) {
    };

    // read till the end of the stream
    while ((i = fr.read()) != -1) {
        char c = (char) i;
        System.out.println(c);
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from w  w w  . ja  v  a2  s . co m*/
        String s = "tutorial from java2s.com";

        Reader reader = new StringReader(s);

        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.mark(10);

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.reset();

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        reader.close();

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

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";
    Scanner scanner = new Scanner(new StringReader(s));

    System.out.println(scanner.nextLine());

    // change the radix of the scanner
    scanner.useRadix(32);//from w w  w  . ja  v a2 s  .c  o m

    // display the new radix
    System.out.println(scanner.radix());

    scanner.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    // create new reader
    Reader r = new StringReader("ABCDEF");

    // create new filter reader
    FilterReader fr = new FilterReader(r) {
    };/*from  w  w w. jav  a  2  s  .c om*/

    // tests if the filter reader supports mark()
    boolean bool = fr.markSupported();

    // prints
    System.out.print("If mark() supported? " + bool);

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    BufferedReader br = new BufferedReader(sr);

    // reads and prints BufferedReader
    System.out.println((char) br.read());
    System.out.println((char) br.read());

    // mark invoked at this position
    br.mark(0);//from www  .  j a  v a2  s  .com
    System.out.println("mark() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

    // reset() repositioned the stream to the mark
    br.reset();
    System.out.println("reset() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Reader r = new StringReader("ABCDEF");

    // create new filter reader
    FilterReader fr = new FilterReader(r) {
    };//from w w  w  .  j av  a2  s. c om

    // reads and prints FilterReader
    System.out.println((char) fr.read());
    System.out.println((char) fr.read());

    // mark invoked at this position
    fr.mark(0);
    System.out.println("mark() invoked");
    System.out.println((char) fr.read());
    System.out.println((char) fr.read());

    // reset() repositioned the stream to the mark
    fr.reset();
    System.out.println("reset() invoked");
    System.out.println((char) fr.read());
    System.out.println((char) fr.read());

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    // create new buffered reader
    BufferedReader br = new BufferedReader(sr);

    // Destination source is created
    CharBuffer target = CharBuffer.allocate(s.length());

    // ready is invoked to test if character stream is ready
    if (br.ready()) {
        br.read(target);//www .ja  v  a2 s  .c  om
    }
    System.out.print(target.array());

}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    CharBuffer cb = CharBuffer.allocate(100);

    Reader reader = new StringReader(s);

    try {/*from  ww w . j a v  a  2 s  . c o  m*/
        reader.read(cb);

        cb.flip();

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

        reader.close();

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

From source file:Main.java

public static void main(String[] args) throws Exception {

    char[] cbuf = new char[6];
    int i = 0;// w ww  .j a  v  a2  s  .  com

    Reader r = new StringReader("ABCDEF");

    FilterReader fr = new FilterReader(r) {
    };

    // read data of len 4, to the buffer
    i = fr.read(cbuf, 2, 4);
    System.out.println("No. of characters read: " + i);

    // read till the end of the char buffer
    for (char c : cbuf) {
        // checks for the empty character in buffer
        if ((byte) c == 0)
            c = '-';

        System.out.print(c);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int i = 0;//from   w ww. ja v a 2s.  co  m

    // create new reader
    Reader r = new StringReader("from java2s.com");

    // create new filter reader
    FilterReader fr = new FilterReader(r) {
    };

    // read till the end of the filter reader
    while ((i = fr.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        // prints
        System.out.println("Character read: " + c);

        // number of characters actually skipped
        long l = fr.skip(2);

        // prints
        System.out.println("Character skipped: " + l);
    }

}