Java.io.BufferedWriter.append() Method


Description

The java.io.BufferedWriter.append(CharSequence csq, int start, int end) method appends subsequence defined by the start and the end postions of the specified character sequence to this write.

Declaration

Following is the declaration for java.io.BufferedWriter.append(CharSequence csq, int start, int end) method

public Writer append(CharSequence csq, int start, int end) 

Parameters

  • csq − It is the character sequence to append.

  • start − The index of the first character of the subsequence.

  • end − The index of the next character following the end character of the subsequence.

Return Value

This writer

Exception

  • IndexOutOfBoundException − If end is greater than csq.length(), start or end are negative, or start is greater than end.

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

Example

The following example shows the usage of java.io.BufferedWriter.append(CharSequence csq, int start, int end) method.

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class BufferedWriterDemo {
   public static void main(String[] args) throws IOException {
      
      StringWriter sw = null;
      BufferedWriter bw = null;
      
      CharSequence csq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
      try{
         // create string writer
         sw = new StringWriter();
         
         //create buffered writer
         bw = new BufferedWriter(sw);
         
         // append subsequence of character sequence.
         bw.append(csq, 5, 11);
         bw.append(" ");
         bw.append(csq, 3, 7);
         bw.append(" ");
         bw.append(csq, 2, 8);
         
         // forces out the characters to string writer
         bw.flush();
         
         // string buffer is created
         StringBuffer sb = sw.getBuffer();
         
         //prints the string
         System.out.println(sb);
            
      }catch(IOException e){
         // if I/O error occurs
         e.printStackTrace();
      }finally{
         // releases any system resources associated with the stream
         if(sw!=null)
            sw.close();
         if(bw!=null)
            bw.close();
      }
   }
}

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

FGHIJK DEFG CDEFGH

bufferedWriter_close.htm
Advertisements