Java.io.PrintWriter.write() Method


Advertisements


Description

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

Declaration

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

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

Parameters

  • s − 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.PrintWriter.write() method.

package com.tutorialspoint;

import java.io.*;

public class PrintWriterDemo {

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

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

         // write substrings
         pw.write(s, 0, 5);
         pw.write(s, 6, 5);

         // flush the writer
         pw.flush();

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

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

HelloWorld

java_io_printwriter.htm

Advertisements