
- 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 - FilterWriter write(String str, int off, int len) method
Description
The Java FilterWriter write(String str, int off, int len) method writes a portion of a string to the output stream. It allows you to specify where to start (off) and how many characters to write (len). off is the starting index in the string. len is the number of characters to write from off. Throws IndexOutOfBoundsException if off + len exceeds the string length.
Declaration
Following is the declaration for java.io.FilterWriter.write(String str, int off, int len) method −
public void write(String str, int off, int len)
Parameters
str − Source string to be written to Filter Writer.
off − Offset from which to start reading characters.
len − Number of characters to be written.
Return Value
The method does not return any value.
Exception
IOException − If an I/O error occurs.
Example - Usage of FilterWriter write(String str, int off, int len) method
The following example shows the usage of Java FilterWriter write(String str, int off, int len) method.
FilterWriterDemo.java
package com.tutorialspoint; import java.io.FilterWriter; import java.io.StringWriter; import java.io.Writer; public class FilterWriterDemo { public static void main(String[] args) throws Exception { FilterWriter fw = null; Writer w = null; String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String s = null; try { // create new reader w = new StringWriter(6); // filter writer fw = new FilterWriter(w) { }; // write to filter writer fw.write(str, 5, 7); // get the string s = w.toString(); // print System.out.print("String: "+s); } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases system resources associated with this stream if(w!=null) w.close(); if(fw!=null) fw.close(); } } }
Output
Let us compile and run the above program, this will produce the following result−
String: FGHIJKL
Example - Writing a Substring Using a Custom FilterWriter
The following example shows the usage of Java FilterWriter write(String str, int off, int len) method.
FilterWriterDemo.java
package com.tutorialspoint; import java.io.FilterWriter; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; // Custom FilterWriter implementation class CustomFilterWriter extends FilterWriter { protected CustomFilterWriter(Writer out) { super(out); } } public class FilterWriterDemo { public static void main(String[] args) { try (FilterWriter fw = new CustomFilterWriter(new FileWriter("output.txt"))) { String text = "Hello, FilterWriter Example!"; // Write only "FilterWriter" (starting from index 7, length 12) fw.write(text, 7, 12); System.out.println("Substring written successfully to output.txt."); } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "Hello")
Let us compile and run the above program, this will produce the following result−
Substring written successfully to output.txt.
Explanation
Creates a custom CustomFilterWriter, as FilterWriter is abstract.
Uses write(text, 7, 12) to write only "FilterWriter" from the string.
The first 7 characters ("Hello, ") are skipped.
Example - Writing a Portion of a Sentence Using BufferedWriter
The following example shows the usage of Java FilterWriter write(String str, int off, int len) method.
FilterWriterDemo.java
package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; // Custom FilterWriter implementation class CustomBufferedFilterWriter extends FilterWriter { protected CustomBufferedFilterWriter(Writer out) { super(out); } } public class FilterWriterDemo { public static void main(String[] args) { try (FilterWriter fw = new CustomBufferedFilterWriter(new BufferedWriter(new FileWriter("data.txt")))) { String sentence = "Microservices architecture is powerful!"; // Write "architecture is" (from index 13, length 15) fw.write(sentence, 13, 15); System.out.println("Substring written successfully to data.txt."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Substring written successfully to data.txt.
Explanation
Uses CustomBufferedFilterWriter, which extends FilterWriter with BufferedWriter.
Calls write(sentence, 13, 15), writing only "architecture is".
Skips the first 13 characters ("Microservices ").