Example usage for java.io CharArrayWriter CharArrayWriter

List of usage examples for java.io CharArrayWriter CharArrayWriter

Introduction

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

Prototype

public CharArrayWriter() 

Source Link

Document

Creates a new CharArrayWriter.

Usage

From source file:Main.java

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

    String str = "from java2s.com!";

    CharArrayWriter chw = new CharArrayWriter();

    chw.write(str);/*w ww  .  java  2 s .co m*/

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

}

From source file:Main.java

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

    String str = "from java2s.com!";

    CharArrayWriter chw = new CharArrayWriter();

    // create destination character array writer
    CharArrayWriter chwd = new CharArrayWriter();

    chw.write(str);//w  ww . j a  v a 2s.  c  o  m

    chw.writeTo(chwd);

    // print the destination buffer content as string
    System.out.println(chwd.toString());

}

From source file:Main.java

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

    String str = "from java2s.com!";

    CharArrayWriter chw = new CharArrayWriter();

    // create destination character array writer
    CharArrayWriter chwd = new CharArrayWriter();

    chw.write(str);//from  www .  j  a va  2  s  . c  o m

    chw.writeTo(chwd);

    // print the destination buffer content as string
    System.out.println(chwd.toString());
    System.out.println(chwd.size());

}

From source file:Main.java

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

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

    CharArrayWriter chw = new CharArrayWriter();

    // write character buffer to the writer
    chw.write(ch);//ww  w  . j av  a2 s  .  c om

    // get buffered content as string
    String str = chw.toString();

    // print the string
    System.out.print(str);

}

From source file:Main.java

public static void main(String args[]) throws IOException {
    CharArrayWriter outStream = new CharArrayWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/*from   w ww. j  a va  2s .c  o m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    CharArrayReader inStream;
    inStream = new CharArrayReader(outStream.toCharArray());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}

From source file:CharArrayWriterDemo.java

public static void main(String args[]) throws IOException {
    CharArrayWriter f = new CharArrayWriter();
    String s = "This should end up in the array";
    char buf[] = new char[s.length()];
    s.getChars(0, s.length(), buf, 0);//  w  w w  .j a  v  a2 s  .c  o  m
    f.write(buf);
    System.out.println(f.toString());

    char c[] = f.toCharArray();
    for (int i = 0; i < c.length; i++) {
        System.out.print(c[i]);
    }

    FileWriter f2 = new FileWriter("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++)
        f.write('X');
}

From source file:Main.java

public static String encodeAttribute(String value) {

    CharArrayWriter writer = new CharArrayWriter();

    int size = value.length();

    for (int i = 0; i < size; i++) {

        char c = value.charAt(i);

        switch (c) {

        case '&':
            writer.append(ENCODED_AMPERSAND);
            break;

        case '"':
            writer.append(ENCODED_DOUBLE_QUOTE);
            break;

        case '<':
            writer.append(ENCODED_LESS_THAN);
            break;

        case '>':
            writer.append(ENCODED_GREATER_THAN);
            break;

        default:/*ww w  .j a  v a  2 s  .  c o  m*/
            writer.append(c);
            break;

        }

    }

    return writer.toString();

}

From source file:Main.java

public static char[] toCharArray(CharSequence input) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    write(input, output);/*  w  w  w .  ja  va 2 s. c o  m*/
    return output.toCharArray();
}

From source file:Main.java

/** Pops up a message box, blocking the current thread. */
public static void pause(final Throwable t) {
    final CharArrayWriter caw = new CharArrayWriter();
    t.printStackTrace(new PrintWriter(caw));
    pause(caw.toString());/* w  ww.  ja v  a 2 s.  c  o  m*/
}

From source file:Main.java

/**
 * Reads the XML document and returns it as a {@link String}.
 * /*from  ww w  .jav a2 s .com*/
 * @param url the URL of the XML document
 * 
 * @return the XML document, as a {@link String}
 * 
 * @throws IOException
 */
public static String readXmlDocument(URL url) throws IOException {
    Reader in = new InputStreamReader(url.openStream());
    CharArrayWriter out = new CharArrayWriter();
    FileCopyUtils.copy(in, out);
    return out.toString();
}