Example usage for java.io CharArrayWriter toCharArray

List of usage examples for java.io CharArrayWriter toCharArray

Introduction

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

Prototype

public char[] toCharArray() 

Source Link

Document

Returns a copy of the input data.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    CharArrayWriter chw = new CharArrayWriter();

    String str = "from java2s.com!";

    chw.write(str);/*from   w  ww  .j a v a 2  s.co m*/

    // get array of character from the writer
    char[] ch = chw.toCharArray();

    // for each character in character array
    for (char c : ch) {

        // print character
        System.out.println(c);
    }

}

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);//from w  w  w  .  j  a  va 2s  .  co 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 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));// ww w.  j  a va 2s. co 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:Main.java

public static char[] toCharArray(CharSequence input) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    write(input, output);//from w  w  w.  j  a  v a 2 s. co  m
    return output.toCharArray();
}

From source file:Main.java

public static char[] toCharArray(InputStream input) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    write(input, output);//from  w  w  w. j a v  a  2 s . c o m
    return output.toCharArray();
}

From source file:Main.java

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

From source file:Main.java

/**
 * Filters an XML document./*from  w w  w.ja va2s  . c o m*/
 * 
 * @param source the input source for the XML document
 * @param filter the filter
 * 
 * @return an input source for the resulting document
 * 
 * @throws Exception
 */
public static InputSource filterXml(InputSource source, XMLFilter filter) throws Exception {
    // Create filter, which uses a "real" XMLReader but also removes the
    // attributes
    XMLReader reader = XMLReaderFactory.createXMLReader();
    filter.setParent(reader);

    // Create a Transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    // Transform the source tree, applying the filter, and store the result
    // tree in a character array
    CharArrayWriter writer = new CharArrayWriter();
    t.transform(new SAXSource(filter, source), new StreamResult(writer));

    // Return a new InputSource using the result tree
    return new InputSource(new CharArrayReader(writer.toCharArray()));
}

From source file:ee.ria.xroad.common.util.PasswordStore.java

private static char[] byteToChar(byte[] bytes) throws IOException {
    if (bytes == null) {
        return null;
    }/*  www.  j av a  2s .  co  m*/

    CharArrayWriter writer = new CharArrayWriter(bytes.length);
    WriterOutputStream os = new WriterOutputStream(writer, UTF_8);
    os.write(bytes);
    os.close();

    return writer.toCharArray();
}

From source file:Main.java

public static char[] toCharArray(InputStream input, String encoding) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    write(input, output, encoding);//from  w ww  .ja v  a 2s. co  m
    return output.toCharArray();
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static char[] read(Reader reader) throws IOException {
    CharArrayWriter writer = new CharArrayWriter();
    transfer(reader, writer);//w w  w  . ja va2  s  .  c o  m
    return writer.toCharArray();
}