- 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 - 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().