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) {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

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

    try {//w w w . ja va  2s . co m
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);
        }
        // unread a character
        pr.unread('F');

        // read the next char, which is the one we unread
        char c = (char) pr.read();

        System.out.println(c);

        pr.close();

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

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    PushbackReader pr = new PushbackReader(sr, 20);

    try {//from  w  w  w.  j ava 2 s .  c  o m
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);
        }

        char cbuf[] = { 'w', 'o', 'r', 'l', 'd' };

        pr.unread(cbuf);

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

        pr.close();

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

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   ww  w  . ja v  a 2s  . com

    // true if the filter reader is ready to be read
    boolean bool = fr.ready();

    // prints
    System.out.println("Ready to read? " + bool);

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   w  ww.  ja  va 2  s. c o  m*/
        String s = "from java2s.com";

        StringReader sr = new StringReader(s);

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

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

        pr.close();

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

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {//from  ww  w  .  j  ava2  s  .c om
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print(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[] argv) throws IOException {
    StringReader stringReader = new StringReader("java2s.com");

    System.out.println(stringReader.ready());

    char[] charArray = new char[10];
    stringReader.read(charArray);//from ww w.  j a  va 2  s  .  c om
    System.out.println(Arrays.toString(charArray));

    stringReader.close();
}

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, 200);

}

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);

}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    // create a new StringReader
    Reader reader = new StringReader(s);

    try {/*from   w  w w .j  av  a 2  s  .  c  om*/
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        System.out.println(reader.markSupported());
        reader.close();

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

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) {
    };/*w  w w. j  av  a  2  s .  c o m*/

    // read and print characters one by one
    System.out.println("Char : " + (char) fr.read());
    System.out.println("Char : " + (char) fr.read());
    System.out.println("Char : " + (char) fr.read());

    // mark is set on the filter reader
    fr.mark(0);
    System.out.println("Char : " + (char) fr.read());
    System.out.println("reset() invoked");

    // reset is called
    fr.reset();

    // read and print characters
    System.out.println("char : " + (char) fr.read());
    System.out.println("char : " + (char) fr.read());

}