Home Java Example Java Io Java IO CharArrayWriter
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java IO CharArrayWriter

Advertisement
In this tutorial we will learn about the CharArrayWriter in Java.

Java IO CharArrayWriter

In this tutorial we will learn about the CharArrayWriter in Java.

java.io.CharArrayWriter extends the java.io.Writer. The CharArrayWriter class writes a character buffer to the output stream. This class is acted as a Writer to write the stream. toCharArray() and toString() methods can be used to get the data. close() method of this class has no effect.

Constructor Detail

Constructor
Description
CharArrayWriter() This default constructor is used to create a new CharArrayWriter.
Syntax : public CharArrayWriter()
CharArrayWriter(int initialSize) This constructor is used to create a new CharArrayWriter with the given initial size.
Syntax : public CharArrayWriter(int initialSize)

Method Detail

  • append(char c) : This method is used to write the characters with appending characters into the stream.

    Syntax : public CharArrayWriter append(char c)
     
  • append(CharSequence csq) : This method is used to write the sequence of characters with appending characters into the stream.

    Syntax : public CharArrayWriter append(CharSequence csq)
     
  • append(CharSequence csq, int start, int end) : This method is used to write the specified part of a sequence of characters.

    Syntax : public CharArrayWriter append(CharSequence csq, int start, int end)
     
  • close() : This method is used to close the stream.s

    Syntax : public void close()
     
  • flush() : This method is used to flush the stream

    Syntax : public void flush()
     
  • reset() : This method is used to reset the stream.

    Syntax : public void reset()
     
  • size() : This method is used to find out the size of buffer.

    Syntax : public int size()
     
  • toCharArray() : This method is used to find out the written data.

    Syntax : public char[] toCharArray()
     
  • toString() : This method is used to convert the input data to string.

    Syntax : public String toString()
     
  • write(char[] c, int off, int len) : This method is used to write a specified length of characters of an array of characters started from offset 'off'.

    Syntax : public void write(char[] c, int off, int len)
     
  • write(int c) : This method is used to write a character to the buffer.

    Syntax : public void write(int c)
     
  • write(String str, int off, int len) : This method is used to write the specified length of string of a specified string started at the offset 'off'.

    Syntax : public void write(String str, int off, int len)
     
  • writeTo(Writer out) : This method is used to write the data of buffer to a character stream.

    Syntax : public void writeTo(Writer out) throws IOException

Example

This example is being given here for demonstrating how to use CharArrayWriter. This example explains that how to write char array to the output stream using, read the characters, and write the data to the file. For this in the example I have used the methods of CharArrayWriter such as write() method (for writing to the output stream), toCharArray() (for reading the char array data), writeTo() (for writing the data to another Writer).

Source Code

import java.io.CharArrayWriter;
import java.io.FileWriter;
import java.io.IOException;

public class JavaCharArrayWriterExample
{
public static void main(String args[])
{
char ch[] = {'a', 'b', 'c', 'd'};
CharArrayWriter caw = null;
FileWriter fw = null;
try
{
caw = new CharArrayWriter(); 
System.out.println();
System.out.println("*****Output*****");
System.out.println("An array of character is written successfully to the output stream");
caw.write(ch);
char c[] = caw.toCharArray();
System.out.print("Data read through toCharArray is : ");
for(int i =0; i<c.length; i++)
{
System.out.print(c[i]);
}
System.out.println();
fw = new FileWriter("test.txt");
caw.writeTo(fw); 
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
if(caw != null)
{
try
{
caw.close();
fw.close();
}
catch(Exception ioe)
{
System.out.println(ioe);
}
}
}// end finally
}// end main
}// end class

Output

When you will execute the above example you will get the output as below as well as you will see that a file with the name which you have specified at the time of programming is created in the specified directory.

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 20, 2012

Related Tutorials

Ask Questions?    Discuss: Java IO CharArrayWriter  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com