Java file Writer
File Writer:
It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant of writing a set of characters. It creates an output file.
Writer Class:
It is a class of Java.io package, it represents the set of characters. It is an abstract class.
Writer Subclasses:
- Buffered Writer
- File Writer
- String Writer
- Output Stream Writer
Writer class object creation Syntax:
Writer object1 = new FileWriter();
Note: Here, to create an object we use FileWriter because the Writer class is abstract.
Constructors in File Writer:
- FileWriter (File f)
- FileWriter (File f, boolean ap)
- FileWriter (FileDescriptor fdes)
- FileWriter (String fN)
- FileWriter (String fN, Boolean ap)
- FileWriter (File f, Charset chset)
- FileWriter (File f, Charset chset, boolean ap)
- FileWriter (String fN, Charset chset, boolean ap)
FileWriter (File f):It creates or generates a file Writer object. It throws n IOException i.e, Input or Output Exception.
Syntax for creating object:
FileWriter f = new FileWriter(File f);
FileWriter (File f, boolean ap): It creates or generates a file writer object. If the given second argument is true then, it will be written the end of the file rather than at the beginning. It throws an IOException i.e, Input or Output Exception.
Syntax for creating object:
FileWriter f = new FileWriter(File f, boolean ap);
FileWriter (FileDescriptor fdes):It creates or generates a file writer object which is associated with the file descriptor.
Syntax for creating object:
FileWriter f = new FileWriter(FileDescriptor fdes);
FileWriter (String fN):It creates or generates a file writer object, given the file name.
Syntax for creating object:
FileWriter f = new FileWriter(String fN);
FileWriter (String fN, Boolean ap): It creates or generates a file writer object given a file name, it indicates whether to append or not to append the data written.
Syntax for creating object:
FileWriter f = new FileWriter(String fN, Boolean ap);
FileWriter (File f, Charset chset): It creates or generates the file object when the file and charset are available.
Syntax for creating object:
FileWriter f = new FileWriter(File f, Charset chset);
FileWriter (File f, Charset chset, boolean ap): It creates or generates the file object when the file and charset are given and if the argument is true then it appends to the end of the file rather than the beginning.
Syntax for creating object:
FileWriter f = new FileWriter(File f, Charset chest, boolean ap);
FileWriter (String fN, Charset chest, boolean ap): It creates a file object when file and charset are given.
Syntax for creating object:
FileWriter f = new FileWriter(String fN, Charset chest, boolean ap);
Methods in File Writer:
- write (int n)
- write (String s1, int pos1, int si)
- write (char c[], int po1, int si)
write (int n):
It is used to write a single character of data. The return type is a void, it overrides the write method which is present in the Writer class. It accepts only one parameter i.e, Integer type. It throws IOException which is an Input / Output Exception when an error occurs in Input or Output.
Syntax:
public void write(int n)
Program:
import java.io.FileWriter;
class Example1 {
public static void main(String args[])
{
String data = "Welcome to Javatpoint";
try {
FileWriter output = new FileWriter("output.txt");
output.write(data);
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Output:
Welcome to Javatpoint
Here, the string is written into the file output.txt then the output.txt file contains Welcome to Javatpint.
write (String s1, int pos1, int si):
It is used to write the part of the string when it is passed through the Writer stream. It throws Input / Output Exception and Index out of bounds exception. I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than string.
Syntax:
public int read(String s1, int pos1, int si)
- s1 -It is the Destination buffer
- pos1 – at which index it starts writing characters.
- si -.Several characters
Program:
import java.io.*;
class C{
public static void main(String[] args)
{
String s1 = “Welcome to JavaTpoint”
try {
FileWriter output = new FileWriter("output.txt");
output.write(s1,0,4);
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Output:
Welcome
write (char c[], int po1, int si):
It writes the characters from a particular array. , it directly uses the main Stream. It throws Input / Output Exception and Index out of bounds exception. I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than an array.
Syntax:
public int read(char[] c, int po1, int si)
- c -It is the Destination buffer
- po1 – at which index it starts writing characters.
- si -.Several characters
Program:
import java.io.*;
class C{
public static void main(String[] args)
{
char ch[] = [‘W’,’E’,’L’,’C’,’O’,’M’,’E’];
try {
FileWriter output = new FileWriter("output.txt");
output.write(ch,0,2);
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Output:
WE