Example usage for java.io StringWriter getBuffer

List of usage examples for java.io StringWriter getBuffer

Introduction

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

Prototype

public StringBuffer getBuffer() 

Source Link

Document

Return the string buffer itself.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(
            new StringReader("<emp><empname><firstName></firstName><lastName></lastName></empname></emp>")));
    NodeList customerNodes = doc.getElementsByTagName("empname");
    for (int i = 0; i < customerNodes.getLength(); i++) {
        NodeList children = customerNodes.item(i).getChildNodes();
        for (int j = 0; j < children.getLength(); j++) {
            String childNode = children.item(j).getNodeName();
            if (childNode.equalsIgnoreCase("firstName")) {
                children.item(j).setTextContent(String.valueOf("John"));
            } else if (childNode.equalsIgnoreCase("lastName")) {
                children.item(j).setTextContent(String.valueOf("Doe"));
            }/*from   ww  w  . j  a  v a 2  s  . c  om*/
        }
    }
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    System.out.println(writer.getBuffer().toString());
}

From source file:Main.java

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

    char[] cbuf = "from java2s.com".toCharArray();

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    bw.write(cbuf, 2, 5);//from  w w w  .j a  va 2s  .  co m

    bw.flush();

    StringBuffer sb = sw.getBuffer();

    System.out.println(sb);

}

From source file:Main.java

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

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    bw.append("java2s.com");

    bw.close();// w  ww  .  j  a va2s. c o  m

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

    // appending after closing will throw error
    bw.append("2");

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

}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    // write a string
    sw.write("tutorial from java2s.com");

    // print result by getting the buffer
    System.out.println("" + sw.getBuffer());

}

From source file:Main.java

public static void main(String[] args) {
    JsonObject personObject = Json.createObjectBuilder().add("name", "Jack").add("age", 13)
            .add("isMarried", false)
            .add("address",
                    Json.createObjectBuilder().add("street", "Main Street").add("city", "New York")
                            .add("zipCode", "11111").build())
            .add("phoneNumber",
                    Json.createArrayBuilder().add("00-000-0000").add("11-111-1111").add("11-111-1112").build())
            .build();// w w  w. j  av a  2  s  .  c  om

    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = Json.createWriter(stringWriter);
    writer.writeObject(personObject);
    writer.close();
    System.out.println(stringWriter.getBuffer().toString());
}

From source file:Main.java

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

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    for (int i = 65; i <= 90; i++) {
        bw.write(i);//from  w w w .j av  a2 s .  co m
    }

    bw.flush();

    StringBuffer sb = sw.getBuffer();

    System.out.println(sb);

}

From source file:Main.java

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

    String letters = "from java2s.com";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    for (char c : letters.toCharArray()) {

        bw.append(c);/*  w  w  w . j  a  v a2s .  co  m*/

        bw.flush();

        System.out.println(sw.getBuffer());
    }

}

From source file:Main.java

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

    String s = "from java2s.com!";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    bw.write(s, 0, 5);/*from  ww  w.j a v a  2 s  .c o  m*/

    bw.newLine();

    bw.write(s, 6, s.length() - 6);

    bw.flush();

    System.out.print(sw.getBuffer());

}

From source file:dk.defxws.fedoragsearch.server.GTransformer.java

public static void main(String[] args) {
    int argCount = 2;
    try {//w  w w .j av  a 2 s  . c  o  m
        if (args.length == argCount) {
            File f = new File(args[1]);
            StreamSource ss = new StreamSource(new File(args[1]));
            GTransformer gt = new GTransformer();
            StreamResult destStream = new StreamResult(new StringWriter());
            gt.transform(args[0], ss, destStream);
            StringWriter sw = (StringWriter) destStream.getWriter();
            System.out.print(sw.getBuffer().toString());
        } else {
            throw new IOException("Must supply " + argCount + " arguments.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Usage: GTransformer xsltName xmlFileName");
    }
}

From source file:Main.java

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

    String str = "from java2s.com!";

    // create string writer
    StringWriter sw = new StringWriter();

    // create buffered writer
    BufferedWriter bw = new BufferedWriter(sw);

    // writing string to writer
    bw.write(str, 1, 4);/*w  w  w .  j  ava  2 s .c om*/

    // forces out the characters to string writer
    bw.flush();

    // string buffer is created
    StringBuffer sb = sw.getBuffer();

    System.out.println(sb);

}