Example usage for java.io Writer toString

List of usage examples for java.io Writer toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

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

    Writer w = new StringWriter(6);

    FilterWriter fw = new FilterWriter(w) {
    };/* w w  w  .  j a va2 s  .  co  m*/

    fw.write(65);

    String s = w.toString();

    System.out.println(s);

}

From source file:InputOutputDemoString.java

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

    //Read from a String s as if it were a text file:
    Reader r = new StringReader("abc");
    System.out.println("abc: " + (char) r.read() + (char) r.read() + (char) r.read());
    //Write to a StringBuffer as if it were a text file:
    Writer sw = new StringWriter();
    sw.write('d');
    sw.write('e');
    sw.write('f');
    System.out.println(sw.toString());
}

From source file:Main.java

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

    String str = "from java2s.com";
    Writer w = new StringWriter(6);

    FilterWriter fw = new FilterWriter(w) {
    };/*from   w w w.ja v a2 s.co  m*/

    fw.write(str, 5, 7);

    String s = w.toString();

    System.out.print(s);

}

From source file:Main.java

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

    char[] cbuf = { 'A', 'B', 'C', 'D', 'E', 'F' };

    Writer w = new StringWriter(6);

    FilterWriter fw = new FilterWriter(w) {
    };//  w  w w  .  j  a va2  s. co m

    fw.write(cbuf, 2, 4);

    String s = w.toString();

    System.out.print(s);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Writer w = new StringWriter(6);

    FilterWriter fw = new FilterWriter(w) {
    };//from   ww  w  . ja va2s  .c  o m

    fw.write(65);

    fw.flush();

    String s = w.toString();

    System.out.print(s);
}

From source file:Main.java

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

    Writer w = new StringWriter(6);

    FilterWriter fw = new FilterWriter(w) {
    };//  w  w w  .  jav a2s .c o  m

    fw.write(65);
    fw.write(66);
    fw.write(67);

    String s = w.toString();

    System.out.print(s);

}

From source file:Main.java

public static String objectToXML(Class clazz, Object object) throws JAXBException {
    String xml = null;/*  www .j av  a  2  s.  c  om*/
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Writer w = new StringWriter();
    m.marshal(object, w);
    xml = w.toString();
    return xml;
}

From source file:Main.java

public static final void prettyPrint(Document xml) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    tf.transform(new DOMSource(xml), new StreamResult(out));
    System.out.println(out.toString());
}

From source file:Main.java

public static String formatDocumentAsString(Document document) throws TransformerException {
    Writer out = new StringWriter();
    formatDocument(document, out);// w ww . j av  a2  s .co m
    String formattedXML = out.toString();
    return formattedXML;
}

From source file:Main.java

/**
 * Method to transform an XML document into a pretty-formatted string.
 * @param xml/* w w  w  . j  a  va 2 s. c o m*/
 * @return
 * @throws Exception
 */
public static final String xmlToString(Document xml) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    tf.transform(new DOMSource(xml), new StreamResult(out));
    return out.toString();
}