Java - FileWriter write(String str, int offset, int len) method



Description

The Java FileWriter write(String str, int offset, int len) method writes a specific portion of a String to a file.

Declaration

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

public void write(String str, int offset, 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 any value.

Exception

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

IOException − If an I/O error occurs.

Example - Writing a Substring to a File

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

FileWriterDemo.java

package com.tutorialspoint;

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

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

         String data = "Hello Java Programming";
         writer.write(data, 6, 4); // Writes "Java" to the file

         writer.close();
         System.out.println("Data written successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written successfully.

Explanation

  • The string "Hello Java Programming" is passed to the write() method.

  • The method starts writing from index 6 and writes 4 characters ("Java").

  • The result in example.txt will be: Java

Example - Writing a Specific Section of a Sentence

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

FileWriterDemo.java

package com.tutorialspoint;

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

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

         String data = "Welcome to Java FileWriter!";
         writer.write(data, 11, 12); // Writes "Java FileWri" to the file

         writer.close();
         System.out.println("Data written successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written successfully.

Explanation

  • The method starts from index 11 ('J' in "Java") and writes 12 characters ("Java FileWri").

  • The result in example.txt will be: Java FileWri

Example - Handling Dynamic Input

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

FileWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileWriterDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string:");
        String input = scanner.nextLine();

        try {
            FileWriter writer = new FileWriter("example.txt");

            writer.write(input, 2, input.length() - 2); // Skips first 2 characters and writes the rest

            writer.close();
            System.out.println("Data written successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
    }
}

Output

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

Data written successfully.

Explanation

  • The first two characters are skipped, and the rest of the string is written to the file.

  • Example input: "Hello World!"

  • The output in example.txt: "llo World!"

java_io_filewriter.htm
Advertisements