Example usage for java.io StringWriter StringWriter

List of usage examples for java.io StringWriter StringWriter

Introduction

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

Prototype

public StringWriter() 

Source Link

Document

Create a new string writer using the default initial string-buffer size.

Usage

From source file:Main.java

public static String getStackTrace(Throwable aThrowable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    aThrowable.printStackTrace(printWriter);
    return result.toString();
}

From source file:Main.java

public static String getCurrentThreadStack() {
    StringWriter sw = new StringWriter();
    new Throwable("").printStackTrace(new PrintWriter(sw));
    return "\n" + sw.toString();
}

From source file:Main.java

public static String getStackTrace(Throwable exception) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    exception.printStackTrace(pw);// w ww.  ja  va  2  s .  c o m
    return sw.toString();

}

From source file:Main.java

static void printE(String tag, Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);//w ww. j  a  v  a2 s. c  om
    Log.e(tag, sw.toString());
}

From source file:Main.java

public static String inputStreamToString(InputStream inputStream) throws IOException {
    StringWriter sw = new StringWriter();
    int c;//from   w  w w . j a  v  a 2  s . com
    InputStream in = inputStream;
    while ((c = in.read()) != -1) {
        sw.write(c);
    }
    return sw.toString();
}

From source file:Main.java

public static String getSetMethod(Class<?> c, String prefix, String postfix) {
    StringWriter sw = new StringWriter();
    if (prefix == null)
        prefix = "";
    if (postfix == null)
        postfix = "";
    Method[] ms = c.getDeclaredMethods();
    if (ms != null && ms.length > 0) {
        for (Method m : ms) {
            String name = m.getName();
            if (name.startsWith("set") && isPublicNoStatic(m.getModifiers())) {
                sw.append(prefix).append(name).append("(").append(postfix).append(");\r\n");
            }/*from  w  ww  . j a v a 2 s.  com*/
        }
    }
    return sw.toString();
}

From source file:Main.java

public static String convertToXmlString(Object source) {
    try {// ww  w. ja va2  s  .co m
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContext = JAXBContext.newInstance(source.getClass());
        Marshaller marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getStackTrace(Throwable aThrowable) {
    aThrowable.printStackTrace();//from  ww  w  .j  a  va 2 s .co  m
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    aThrowable.printStackTrace(printWriter);
    return result.toString();
}

From source file:Main.java

public static String getXmlFileToString(String filePath) throws TransformerException {
    File xmlFile = new File(filePath);
    StringWriter writer = new StringWriter();
    TransformerFactory fac = TransformerFactory.newInstance();
    Transformer x = fac.newTransformer();
    x.transform(new StreamSource(xmlFile), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String getStackTraceString(Throwable t) {
    String value;// www.j a  va2  s .  c om
    if (t == null) {
        value = "";
    } else {
        final StringWriter stackTrace = new StringWriter();
        t.printStackTrace(new PrintWriter(stackTrace));
        value = stackTrace.toString();
    }
    return value;
}