Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Order o = new Order();
    o.setCustId(123);/*from www  . j  a  va 2 s  . c  om*/
    o.setDescription("New order");
    o.setOrderDate(new Date());

    List<Item> items = new ArrayList<Item>();

    Item i = new Item();
    i.setName("PC");
    i.setQty(10);
    items.add(i);

    i = new Item();
    i.setName("Box");
    i.setQty(4);

    items.add(i);

    o.setItems(items);
    // Write it
    JAXBContext ctx = JAXBContext.newInstance(Order.class);

    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    m.marshal(o, sw);
    sw.close();
    System.out.println(sw.toString());

    // Read it back
    JAXBContext readCtx = JAXBContext.newInstance(Order.class);
    Unmarshaller um = readCtx.createUnmarshaller();

    Order newOrder = (Order) um.unmarshal(new StringReader(sw.toString()));
    System.out.println(newOrder);
}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    // create a new sequence
    String s = "from java2s.com";

    // write a string
    sw.write(s);//from  w  w  w  .  j  ava 2s  .  co m

    // flush the writer
    sw.flush();

    System.out.println(sw.toString());

}

From source file:net.jcreate.e3.templateEngine.jxp.JxpTemplateEngine.java

public static void main(String[] args) {
    JxpTemplateEngine a = new JxpTemplateEngine();
    Template t = new DefaultTemplate();
    t.setResource("test.jxp");
    Context c = new DefaultContext();
    c.put("msg", "hello");
    c.put("msg2", ".");
    StringWriter writer = new StringWriter();
    a.mergeFileTemplate(t, c, writer);/*  w  w w  .  java2  s  . c  o m*/
    System.out.println(writer.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from  w w  w . j a  va2  s . c o m*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    Element root = doc.createElementNS(null, "person"); // Create Root Element
    Element item = doc.createElementNS(null, "name"); // Create element
    item.appendChild(doc.createTextNode("Jeff"));
    root.appendChild(item); // Attach element to Root element
    item = doc.createElementNS(null, "age"); // Create another Element
    item.appendChild(doc.createTextNode("28"));
    root.appendChild(item); // Attach Element to previous element down tree
    item = doc.createElementNS(null, "height");
    item.appendChild(doc.createTextNode("1.80"));
    root.appendChild(item); // Attach another Element - grandaugther
    doc.appendChild(root); // Add Root to Document

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                       // for the DOM
    LSOutput out = domImplLS.createLSOutput();
    StringWriter stringOut = new StringWriter(); // Writer will be a String
    out.setCharacterStream(stringOut);
    ser.write(doc, out); // Serialize the DOM

    System.out.println("STRXML = " + stringOut.toString()); // DOM as a String
}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    // create two new sequences
    CharSequence sq1 = "Hello";
    CharSequence sq2 = " World from java2s.com";

    // append sequence
    sw.append(sq1);/*from  w  w  w.j av  a  2s .co  m*/

    System.out.println(sw.toString());

    // append second sequence
    sw.append(sq2);

    System.out.println(sw.toString());

}

From source file:Main.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter();
    String s = "This is a test from j a va2s.com.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//w ww .ja  va  2 s.  co  m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
}

From source file:StringIOApp.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from   w  ww  .j a v a 2s  .c  om
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
    StringReader inStream;
    inStream = new StringReader(outStream.toString());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();

    StringWriter sw = new StringWriter();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(sw);
    xsw.writeStartDocument();/*from  ww  w  .ja  v a 2s . c o  m*/
    xsw.writeStartElement("foo");
    xsw.writeCharacters("<>\"&'");
    xsw.writeEndDocument();

    String xml = sw.toString();
    System.out.println(xml);

    // READ THE XML
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
    xsr.nextTag(); // Advance to "foo" element
    System.out.println(xsr.getElementText());
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter(100);
    String s = "This is a test from j a va2s.com.";
    for (int i = 0; i < s.length(); ++i) {
        outStream.write(s.charAt(i));//from   ww w  . j av a2  s .co m
    }
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
}

From source file:DOMGenerate.java

public static void main(String[] argv) {
    try {//w ww  .  j  ava  2s. co  m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        Element root = doc.createElementNS(null, "person"); // Create Root Element
        Element item = doc.createElementNS(null, "name"); // Create element
        item.appendChild(doc.createTextNode("Jeff"));
        root.appendChild(item); // Attach element to Root element
        item = doc.createElementNS(null, "age"); // Create another Element
        item.appendChild(doc.createTextNode("28"));
        root.appendChild(item); // Attach Element to previous element down tree
        item = doc.createElementNS(null, "height");
        item.appendChild(doc.createTextNode("1.80"));
        root.appendChild(item); // Attach another Element - grandaugther
        doc.appendChild(root); // Add Root to Document

        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

        LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                           // for the DOM
        LSOutput out = domImplLS.createLSOutput();
        StringWriter stringOut = new StringWriter(); // Writer will be a String
        out.setCharacterStream(stringOut);
        ser.write(doc, out); // Serialize the DOM

        System.out.println("STRXML = " + stringOut.toString()); // Spit out the
                                                                // DOM as a String
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}