Java FilterWriter write() method
In this tutorial, we will learn about write()
method from FilterWriter
class in Java. We use the write() method to write the data to the FilterWriter, we have three different overloading methods of the write() method.
Syntax
This method comes in three overloaded forms to support various data and their sliced parts.
This overloaded method will receive the three parameters, an array of characters, offset which indicates the starting position in the data, and the last parameter as the length of the data, and this way it writes a portion of an array of characters.
void write(char[] cbuf, int off, int len)
Here is another overloaded method that takes an integer as an ASCII code for the character and writes it to the stream, it writes a single character.
void write(int c)
This is another overloaded method that will accept the string as a source data, offset as a starting position from where it will start writing and the length of the data to be written in the file.
void write(String str, int off, int len)
Example of write() Method
In the following program, we are writing the data from the string array str, since we passed offset value 2 so it will start from reading 3rd index after skipping 0, 1, and 2 indexes and the length of the text will be 8 characters long.
import java.io.FileWriter;
import java.io.FilterWriter;
public class StudyTonight
{
public static void main(String args[])
{
try
{
char str[]={'s', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'};
FileWriter fileWriter = new FileWriter("E:\\studytonight\\output.txt");
FilterWriter filterWriter = new FilterWriter(fileWriter) {};
filterWriter.write(str, 2, 8);
filterWriter.close();
System.out.println("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Data is written to the file successfully...
output.txt
udytonig
Example of write() Method
In this example, we are looking at the simplest overloading method of write() and it will accept integer as an ASCII code and it will write it to the output stream.
import java.io.FileWriter;
import java.io.FilterWriter;
public class StudyTonight
{
public static void main(String args[])
{
try
{
FileWriter fileWriter = new FileWriter("E:\\studytonight\\output.txt");
FilterWriter filterWriter = new FilterWriter(fileWriter) {};
filterWriter.write(65);
filterWriter.close();
System.out.println("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Data is written to the file successfully...
output.txt
A
Example of write() Method
This example is similar to the first example where we used character array the only difference is here we used the string instead of an array and we are passing 2 as ana offset and 8 is the length of the string to be written in the output stream.
import java.io.FileWriter;
import java.io.FilterWriter;
public class StudyTonight
{
public static void main(String args[])
{
try
{
String str = "studytonight";
FileWriter fileWriter = new FileWriter("E:\\studytonight\\output.txt");
FilterWriter filterWriter = new FilterWriter(fileWriter) {};
filterWriter.write(str, 2, 8);
filterWriter.close();
System.out.println("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Data is written to the file successfully...
output.txt
udytonig
Conclusion
In this tutorial, we learned about write() method of FilterWriter in Java. This method comes in the three overloading method, void write(char[] cbuf, int off, int len)
, void write(int c
) and void write(String str, int off, int len)
method.