Java - FilterWriter write(char[] cbuf, int off, int len) method



Description

The Java FilterWriter write(char[] cbuf, int off, int len) method writes a portion of a character array to the output stream. It allows you to specify where to start and how many characters to write. Writes len characters from cbuf, starting at index off.

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 - Usage of FilterWriter write(char[] cbuf, int off, int len) method

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

FilterWriterDemo.java

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();
      }
   }
}

Output

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

String: CDEF

Example - Writing a Subset of Characters Using BufferedWriter

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

FilterWriterDemo.java

package com.tutorialspoint;

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

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (FilterWriter fw = new FilterWriter(new FileWriter("output.txt")){}) {
         char[] data = "Hello, FilterWriter!".toCharArray();

         fw.write(data, 7, 12); // Writes "FilterWriter"
         System.out.println("Selected portion written to output.txt.");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Selected portion written to output.txt.

Explanation

  • Converts the string "Hello, FilterWriter!" into a character array.

  • Calls write(data, 7, 12), which writes only "FilterWriter".

  • The first 7 characters ("Hello, ") are skipped.

Example - Writing a Subset of Data in Chunks Using PrintWriter

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

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (FilterWriter fw = new FilterWriter(new FileWriter("data.txt")){}) {
         char[] text = "Microservices are powerful!".toCharArray();

         // Write "services are" (from index 5, length 12)
         fw.write(text, 5, 12);
         System.out.println("Subset of characters written to data.txt.");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Subset of characters written to data.txt.

Explanation

  • Converts "Microservices are powerful!" into a character array.

  • Calls write(text, 5, 12), which writes only "services are".

  • The first 5 characters ("Micro") are skipped.

java_io_filterwriter.htm
Advertisements