Java.io.StringWriter.write() Method



Description

The java.io.StringWriter.write() method writes a portion of a string.

Declaration

Following is the declaration for java.io.StringWriter.write() method.

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

Parameters

  • str − String to be written.

  • off − Offset from which to start writing characters.

  • len − Number of characters to write.

Return Value

This method does not return a value.

Exception

NA

Example

The following example shows the usage of java.io.StringWriter.write() method.

package com.tutorialspoint;

import java.io.*;

public class StringWriterDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new writer
      StringWriter sw = new StringWriter();

      // write strings
      sw.write(s, 0, 4);
      sw.write(s, 5, 6);

      // print result by converting to string
      System.out.println("" + sw.toString());
   }
}

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

Hello World
java_io_stringwriter.htm
Advertisements