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



Description

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

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(OutputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.method flushes the stream.

Declaration

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

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

Parameters

  • str − A String.

  • 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.

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

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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

      try {
         // create a new OutputStreamWriter
         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(s, 0, 5);

         // flush the stream
         writer.flush();

         // read what we write
         for (int i = 0; i < 5; i++) {
            System.out.print("" + (char) in.read());
         }

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

Output

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

Hello

Example - Writing part of a string to a file

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("string_output1.txt"));

         String message = "Welcome to Java OutputStreamWriter";

         // Write only "Java OutputStreamWriter" (starts at index 11, length 23)
         writer.write(message, 11, 23);

         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

  • write(String str, int off, int len) writes a substring starting at off (offset) for len characters.

  • "Welcome to Java OutputStreamWriter" → index 11 is where "Java" starts.

  • The result is "Java OutputStreamWriter" written to the file.

Example - Writing a substring to console output

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try (OutputStreamWriter writer = new OutputStreamWriter(System.out)) {
         String info = "OutputStreamWriter is a bridge from character streams to byte streams.";

         // Write only "bridge from character" (starts at index 23, length 23)
         writer.write(info, 23, 23);
         writer.write("\n");  // Add a newline
         writer.flush();     // Ensure the output is shown immediately
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

bridge from character

Explanation

  • This writes a specific portion of the string directly to the console.

  • flush() is called to make sure the output is shown immediately.

  • This is useful for dynamic console applications or streaming logs/messages.

java_io_outputstreamwriter.htm
Advertisements