Open In App

Java.io.Writer class in Java

This abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
Constructor

Methods:



Program :




//Java program demonstrating Writer methods
  
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
class WriterDemo
{
    public static void main(String[] args) throws IOException
    {
        Writer wr=new PrintWriter(System.out);
        char c[] = {'B','C','D','E','F'};
        CharSequence cs = "JKL";
        String str = "GHI";
  
        //illustrating write(int c)
        wr.write(65);
          
        //flushing the stream
        wr.flush();
          
        //illustrating write(char[] c,int off,int len)
        wr.write(c);
        wr.flush();
          
        //illustrating write(String str,int off,int len)
        wr.write(str);
        wr.flush();
          
        //illustrating append(Charsequence cs,int start,int end)
        wr.append(cs);
        wr.flush();
          
        //illustrating append(int ch)
        wr.append('M');
        wr.flush();
  
        //closing the stream
        wr.close();
    }
}

Output :

ABCDEFGHIJKLM

Article Tags :