
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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!"