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

    try {/*from w  w  w . j  a v  a 2s  . co  m*/
        // read the first five chars
        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 {
    StringReader reader = new StringReader("this is a test");

    int wordCount = 0;
    StreamTokenizer streamTokenizer = new StreamTokenizer(reader);

    while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
        if (streamTokenizer.ttype == StreamTokenizer.TT_WORD)
            wordCount++;//from  w ww.j  a  va  2 s . co  m
    }
    System.out.println("Number of words in file: " + wordCount);
}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {/*  w ww  .jav  a 2  s.c  om*/
        // check if reader is ready
        System.out.println(reader.ready());

        // read the first five chars
        for (int i = 0; i < 5; 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 = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

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

    try {/*from w  ww . ja  v a  2  s  .  c  o  m*/
        // read characters into a portion of an array.
        System.out.println(reader.read(cbuf, 0, 5));

        System.out.println(cbuf);

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

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

    try {//from   w  ww . ja v  a 2  s  . co m
        // read characters into an array.
        System.out.println(reader.read(cbuf));

        System.out.println(cbuf);
        reader.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);

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

    try {//from   w ww  .j  a  va2 s .  c o  m
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);

            // skip a character every time
            pr.skip(1);
        }

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

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

    try {//from  w w  w. j a va2s.c  om
        // check if the reader is ready
        System.out.println(pr.ready());

        // read the first five chars
        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) {
    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    PushbackReader pr = new PushbackReader(sr, 20);

    try {/* w  w  w .j  a v  a 2s  .  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, 1, 4);

        for (int i = 0; i < 4; 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[] argv) throws IOException {
    StringReader stringReader = new StringReader("java2s.com");

    stringReader.mark(2);// w  ww  . jav  a 2 s  .  co m

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

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

    stringReader.reset();

    stringReader.close();
}

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

    try {/*from   w w w.  j  a  va 2  s .  c  om*/
        // 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();
    }
}