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



Description

The Java Writer write(String str,int off,int len) method writes a portion of a string.

Declaration

Following is the declaration for java.io.Writer.write(String str,int off,int len) 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

  • IOException − If an I/O error occurs.

  • IndexOutOfBoundsException − If off is negative, or len is negative, or off+len is negative or greater than the length of the given string.

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

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      String str = "Hello world!";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write a string portion
         writer.write(str, 0, 5);

         // flush the writer
         writer.flush();

         // write another string portion
         writer.write(str, 5, 6);

         // flush the stream again
         writer.flush();

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

Output

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

Hello world

Example - Using FileWriter with write(String, int, int)

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

WriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new FileWriter("write_partial_string1.txt");

         String text = "Welcome to Java Programming";
         writer.write(text, 11, 4);  // Writes "Java"

         writer.close();
         System.out.println("Partial string written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Partial string written to file.

Explanation

  • "Welcome to Java Programming" is the original string.

  • writer.write(text, 11, 4) writes 4 characters starting from index 11 → "Java".

  • The text is saved to a file named write_partial_string1.txt.

Example - Using StringWriter with write(String, int, int)

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new StringWriter();

         String message = "Buffered writing with StringWriter";
         writer.write(message, 9, 7);  // Writes "writing"

         System.out.println("Output: " + writer.toString());
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Output: writing

Explanation

  • A string is partially written to memory using StringWriter.

  • writer.write(message, 9, 7) writes the word "writing" starting from index 9.

  • The output is displayed using toString().

java_io_writer.htm
Advertisements