Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - CharArrayWriter write(String str, int off, int len) method



Description

The Java CharArrayWriter write(String str, int off, int len) method writes a part of the string to the writer.

Declaration

Following is the declaration for java.io.CharArrayWriter.write(String str, int off, int len) method −

public void write(String str, int off, int len)

Parameters

  • str − Source string.

  • off − Offset to start reading the characters from.

  • len − Number of characters to be written.

Return Value

This method does not return any value.

Exception

NA

Example - Usage of write(String str, int off, int len) method

The following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      String str = "Hello World!!";
      CharArrayWriter chw = null;
            
      try {
         // create character array writer
         chw = new CharArrayWriter();

         // portion to be written to writer
         chw.write(str, 4, 9);
         
         // print the buffer as string
         System.out.println(chw.toString());
               
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

Output

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

o World!!

Example - Writing a Substring to CharArrayWriter

The following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();
      String sentence = "Welcome to Java Programming!";

      // Write "Welcome" (from index 0, length 7)
      writer.write(sentence, 0, 7);

      // Write "Java" (from index 11, length 4)
      writer.write(sentence, 11, 4);

      // Write "!" (from index 26, length 1)
      writer.write(sentence, 26, 1);

      // Convert to string and print
      System.out.println("Written data: " + writer.toString());
   }
}

Output

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

Written data: WelcomeJavag

Explanation

  • The original string is "Welcome to Java Programming!".

  • write(String s, int off, int len) is called three times to write−

    • "Welcome" from index 0 (length 7).

    • "Java" from index 11 (length 4).

    • "!" from index 26 (length 1).

  • The toString() method converts the contents into a string and prints it.

Example - Writing a Partial String from the Middle

The following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();
      String message = "Java Programming Language";

      // Write substring "Programming" (from index 5, length 11)
      writer.write(message, 5, 11);

      // Convert to string and print
      System.out.println("Written data: " + writer.toString());
   }
}

Output

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

Written data: Programming

Explanation

  • A string "Java Programming Language" is used as input.

  • The write(String s, int off, int len) method writes "Programming" (starting at index 5 with length 11).

  • The final output is obtained using toString().

java_io_chararraywriter.htm
Advertisements