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 www .j a  v  a 2  s.c o m
    char c;

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

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

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

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

}

From source file:Main.java

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

    String s = "from java2s.com";

    // create and assign a new string reader
    StringReader sr = new StringReader(s);

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

    // reads and prints BufferedReader
    int value = 0;
    while ((value = br.read()) != -1) {
        // skips a character
        br.skip(1);/*from   w  ww  .  jav  a2  s  . c  o  m*/
        System.out.print((char) value);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String str = "This is a  test, 200.89  which  is  simple 50";
    StringReader sr = new StringReader(str);
    StreamTokenizer st = new StreamTokenizer(sr);
    try {/*from www .  j a v  a 2  s  . c o  m*/
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            switch (st.ttype) {
            case StreamTokenizer.TT_WORD: /* a word has been read */
                System.out.println("String value: " + st.sval);
                break;
            case StreamTokenizer.TT_NUMBER: /* a number has been read */
                System.out.println("Number value:  " + st.nval);
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader("<foo><bar>toast</bar></foo>");
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    System.out.println(foo.isBar());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Properties prop = new Properties();
    String s = "Chapter Count=200\nTutorial Count=15";

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

    // load from input stream
    prop.load(reader);// ww  w.j a  va 2  s.  c o m

    // print the properties list from System.out
    prop.list(System.out);

}

From source file:MainClass.java

License:asdf

public static void main(String[] args) throws IOException {
    try {/* ww  w. ja va 2s  . c o  m*/
        BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
        PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
        int lineCount = 1;
        String s = null;
        while ((s = in4.readLine()) != null)
            out1.println(lineCount++ + ": " + s);
        out1.close();
    } catch (EOFException e) {
        System.err.println("End of stream");
    }

}

From source file:Main.java

public static void main(String[] args) {

    String simpleXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Bob</name></person>";
    InputSource inputSource = new InputSource(new StringReader(simpleXML));
    XPath xpath = XPathFactory.newInstance().newXPath();

    try {/*  w w  w .j  a  va2s .  c  o m*/
        String name = xpath.evaluate("//name", inputSource);
        System.out.println(name);
    } catch (XPathExpressionException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<a><o><u>ok</u></o></a>";
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));
    NodeList nl = doc.getElementsByTagName("*");
    for (int i = 0; i < nl.getLength(); i++) {
        System.out.println("name is : " + nl.item(i).getNodeName());
    }/*from  w w  w  . jav  a  2s  . c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String docroot = "<div><i>items <b>sold</b></i></div>";
    XPath xxpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(docroot));
    String result = (String) xxpath.evaluate("//b", inputSource, XPathConstants.STRING);
    System.out.println(result);/*from   www . j a  v  a2  s. c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader("<foo><bar>toast</bar></foo>");
    Foo foo = (Foo) unmarshaller.unmarshal(xml);
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override/*from ww w  .j ava  2 s  . co  m*/
        public boolean handleEvent(ValidationEvent ve) {
            System.out.println(ve.getMessage());
            return true;
        }
    });

    System.out.println(foo.isBar());
}