Java.io.FilterWriter.write() Method

Advertisements


Description

The java.io.FilterWriter.write(char[] cbuf, int off, int len) method writes a portion of len number of characters to the file writer, read() from the offset off of the character array.

Declaration

Following is the declaration for java.io.FilterWriter.write(char[] cbuf, int off, int len) method:

public void write(char[] cbuf, int off, int len)

Parameters

  • cbuf -- Source character buffer to be written to this filter writer.

  • off -- Offset from which to start reading characters.

  • len -- Number of characters to be written.

Return Value

The method does not return any value.

Exception

  • IOException -- If an I/O error occurs.

Example

The following example shows the usage of java.io.FilterWriter.write(char[] cbuf, int off, int len) method.

package com.tutorialspoint;

import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterDemo {
   public static void main(String[] args) throws Exception {
      
      FilterWriter fw = null;
      Writer w = null;
      char[] cbuf={'A','B','C','D','E','F'};
      String s=null;
      
      try{
         // create new reader
         w = new StringWriter(6);
          
         // filter writer
         fw = new FilterWriter(w) {
         };
         
         // write to filter writer from character buffer
         fw.write(cbuf, 2, 4);

         // get the string
         s = w.toString();
         
         // print
         System.out.print("String: "+s);
         
      }catch(Exception e){
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally{
         
         // releases system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null)
            fw.close();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

String: CDEF



Advertisements
Advertisements