Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io Java IO Writer

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO Writer

Advertisement
In this section we will discuss about the Writer class in Java.

Java IO Writer

In this section we will discuss about the Writer class in Java.

java.io.Writer is an abstract class which provides some methods to write characters to the stream. Object of this class can't be created directly. This class is a super class of all the classes which are involved in or to facilitate to write (output) the character streams. Classes descended from this class may overrides the methods of this class but the method write(char[], int, int), flush() and close() must be implemented by the subclasses.

Constructor Detail

Constructor
Description
Writer() This is a default constructor which creates a writer for writing character streams. In such type of character streams writer synchronization of critical sections is depend on the writer itself.
Syntax : protected Writer()
Writer(Object lock) A parameterized constructor which creates a writer for writing character streams. In such type of character streams writer synchronization of critical sections is depend on the given object.
Syntax : protected Writer(Object lock)

Methods Detail

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

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

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

    Syntax : public Writer append(CharSequence csq, int start, int end) throws IOException
     
  • close() : This method is used to close the stream as well as to release the resources associated with the stream.

    Syntax : public abstract void close() throws IOException
     
  • flush() : This method is used to flush out the stream.

    Syntax : public abstract void flush() throws IOException
     
  • write(char[] cbuf) : This method is used to write character's array.

    Syntax : public void write(char[] cbuf) throws IOException
     
  • write(char[] cbuf, 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 abstract void write(char[] cbuf, int off, int len) throws IOException
     
  • write(int c) : This method is used to write a character to the stream.

    Syntax : public void write(int c) throws IOException
     
  • write(String str) : This method is used to write a specified string to the stream.

    Syntax : public void write(String str) throws IOException
     
  • 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) throws IOException

Example

To demonstrate how to use Writer to write into the output stream I am giving a simple example. In this example I have created a Java class named JavaWriterExample.java where I have created an object of Writer using BufferedWriter to write to the output stream and created object of FileWriter to write the stream to the file. Further in the example I have flushed and closed the stream using finally block.

Source Code

JavaWriterExample.java

import java.io.Writer;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

public class JavaWriterExample
{
   public static void main(String args[])
    {
         char[] ch = {'a', 'b', 'c'};
         FileWriter fw = null;
         Writer wr = null;
         //BufferedWriiter bw = null;
         try
           {
              fw = new FileWriter("test.txt");
              wr = new BufferedWriter(fw);
              wr.write("Welcome to Roseindia");
              wr.write("    ");
              wr.write(ch);
              System.out.println("\n File successfully written");
           }
         catch(IOException ioe)
           {
              System.out.println(ioe);
           }
         finally
           {
              if(fw != null)
                {
                   try
                     {
                        fw.flush();
	          wr.flush();  
                        fw.close();
	          wr.close();
                     }
                   catch(Exception e)
                     {
                        System.out.println(e);
                     }
                }
         }//finally closed     
    }// main closed
}// class closed

Output

When you will compile and execute the above example like below then the output will be as follows :

When the output will show like the above then if you will open the directory what you have given the path at the time of creating of file (say test.txt) you will see that the file with the specified name is created and contains the text written by the Java program.

Source code of the above example can be downloaded from the link given below.

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

Discuss: Java IO Writer  

Post your Comment


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