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(int initialSize) 

Source Link

Document

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

Usage

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));//w  w w  . j  ava 2s .c  o m
    }
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
}

From source file:Main.java

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

    FilterWriter fw = new FilterWriter(w) {
    };/*from w  w  w.ja  v  a2  s.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) {
    };/*from w w  w  .  ja v a2s  . c  o  m*/

    fw.write(65);

    String s = w.toString();

    System.out.println(s);

}

From source file:Main.java

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

    Writer w = new StringWriter(6);

    FilterWriter fw = new FilterWriter(w) {
    };// ww 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 void main(String[] args) throws Exception {

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

    FilterWriter fw = new FilterWriter(w) {
    };/*from  www .  j  av a2  s . c o 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 av  a  2s  .c  om

    fw.write(cbuf, 2, 4);

    String s = w.toString();

    System.out.print(s);
}

From source file:com.enitalk.configs.VelocityConfig.java

public static void main(String[] args) throws IOException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(VelocityEngine.class);
    VelocityEngine engine = ctx.getBean(VelocityEngine.class);

    long start = System.currentTimeMillis();
    Template t = engine.getTemplate("booking.vm");

    VelocityContext context = new VelocityContext();
    context.put("scheduledDate", new DateTime().toString());
    context.put("link", "http://localhost:8080");

    StringWriter writer = new StringWriter(24 * 1024);
    t.merge(context, writer);/*  w  ww.  j a v a  2  s  .  c  om*/

    FileUtils.write(new File("/home/krash/Desktop/1.html"), writer.toString(), "UTF-8");
    logger.info("Took {}", System.currentTimeMillis() - start);
    //        String templateText = FileUtils.readFileToString(new File("./templates/booking.vm"), "UTF-8");
    //        logger.info("Templated text {}", templateText);

}

From source file:Main.java

/**
 * dump the exception to string//  w  w  w  .j a  v a  2  s. c  o m
 */
public static String dumpException(Throwable throwable) {
    StringWriter stringWriter = new StringWriter(160);
    stringWriter.write(throwable.getClass().getName());
    stringWriter.write(":\n");
    throwable.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:Main.java

/**
 * @param str/*ww w .  j a  v  a 2  s  .c  o  m*/
 * @return a code snippet ready to copy and paste into java code.
 */
public static String escapedJavaStringConstant(String str) {
    if (str == null) {
        return null;
    }
    try {
        StringWriter writer = new StringWriter(str.length() * 2);
        writer.write("String s = \"");
        escapeJavaStringConstant(writer, str, true);
        writer.write("\";");
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringWriter
        ioe.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static String stringForm(Throwable e) {
    StringWriter sw = new StringWriter(256);
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);//  w w w .j  ava2  s  .  co m
    return sw.getBuffer().toString();
}