Example usage for java.io StringWriter close

List of usage examples for java.io StringWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a StringWriter has no effect.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Order o = new Order();
    o.setCustId(123);//from  w  w  w . j  a va 2  s.  c o m
    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:org.apache.asterix.experiment.action.derived.StartDataGeneratorAction.java

public static void main(String[] args) throws Exception {
    SSHClient sshClient = new SSHClient();
    sshClient.loadKnownHosts();//from   ww  w .ja  v a 2  s  .co m
    sshClient.connect("asterix-1.ics.uci.edu");
    sshClient.authPublickey("zheilbro", "/Users/zheilbron/.ssh/id_rsa");
    Session session = sshClient.startSession();
    Command lsCmd = session.exec("ls");
    StringWriter sw = new StringWriter();
    IOUtils.copy(lsCmd.getInputStream(), sw);
    IOUtils.copy(lsCmd.getErrorStream(), sw);
    System.out.println(sw.toString());
    session.close();
    sw.close();
    sshClient.close();
}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

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

    // write a string
    sw.write(s);//from  w  ww  . ja v a  2  s .  c  o  m

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

    try {
        // close the writer
        sw.close();
    } catch (IOException ex) {
        ex.printStackTrace();
        ;
    }

}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound)
        throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(classesToBeBound);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat);
    m.setSchema(null);//from   w ww  .ja v a  2s. c o m
    StringWriter sw = new StringWriter();
    m.marshal(object, sw);
    String result = sw.toString();
    sw.close();
    return result;
}

From source file:AIR.Common.Json.JsonHelper.java

public static <T> String serialize(T obj) throws JsonGenerationException, JsonMappingException, IOException {
    if (obj == null)
        return null;

    ObjectMapper mapper = new ObjectMapper();

    String json = null;/*from w w  w  .j  a  v  a 2  s  .  c om*/

    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, obj);

    json = sw.toString();
    sw.close();

    return json;
}

From source file:com.jsonstore.jackson.JsonOrgModule.java

public static String serialize(JSONObject obj) {
    try {/*from w ww  .ja v  a 2s.c om*/
        StringWriter writer = new StringWriter();

        JsonOrgModule.mapper.writeValue(writer, obj);

        writer.close();

        return writer.toString();
    }

    catch (Throwable e) {
        return null;
    }
}

From source file:com.jsonstore.jackson.JsonOrgModule.java

public static String serialize(JSONArray array) {
    try {/*from www  . j  a  v  a  2s .c om*/
        StringWriter writer = new StringWriter();

        JsonOrgModule.mapper.writeValue(writer, array);

        writer.close();

        return writer.toString();
    }

    catch (Throwable e) {
        return null;
    }
}

From source file:org.apache.gobblin.util.PropertiesUtils.java

public static String serialize(Properties properties) throws IOException {
    StringWriter outputWriter = new StringWriter();
    properties.store(outputWriter, "");
    String rst = outputWriter.toString();
    outputWriter.close();
    return rst;//w w  w . jav  a2s . c  o  m
}

From source file:com.cloudbees.plugins.credentials.cli.BaseCredentialsCLICommand.java

protected static HierarchicalStreamReader safeXmlStreamReader(Source source) throws IOException {
    final StringWriter out = new StringWriter();
    try {/*from  ww  w  .  j  a v a 2 s  .c  o  m*/
        XMLUtils.safeTransform(source, new StreamResult(out));
        out.close();
    } catch (TransformerException e) {
        throw new IOException("Failed to parse", e);
    } catch (SAXException e) {
        throw new IOException("Failed to parse", e);
    }
    return new XppDriver().createReader(new StringReader(out.toString()));

}

From source file:com.joliciel.csvLearner.utils.LogUtils.java

/**
Return the current exception & stack trace as a String.
*//*from   w ww . j a va2  s  . c om*/
public static String getErrorString(Throwable e) {
    String s = null;
    try {
        StringWriter sw = new StringWriter();
        PrintWriter ps = new PrintWriter((Writer) sw);
        e.printStackTrace(ps);
        sw.flush();
        s = sw.toString();
        sw.close();
    } catch (IOException ioe) {
        // do nothing!
    }
    return s;
}