0% found this document useful (0 votes)
10 views5 pages

Java - FileWriter Class Tutorialspoint

Uploaded by

japariciofdez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Java - FileWriter Class Tutorialspoint

Uploaded by

japariciofdez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8/9/23, 20:52 Java - FileWriter Class

Java - FileWriter Class

This class inherits from the OutputStreamWriter class. The class is used for writing streams
of characters.

This class has several constructors to create required objects. Following is a list.

https://www.tutorialspoint.com/java/java_filewriter_class.htm 1/5
8/9/23, 20:52 Java - FileWriter Class

Sr.No. Constructor & Description

1
FileWriter(File file)

This constructor creates a FileWriter object given a File object.

2
FileWriter(File file, boolean append)

This constructor creates a FileWriter object given a File object with a boolean
indicating whether or not to append the data written.

3
FileWriter(FileDescriptor fd)

This constructor creates a FileWriter object associated with the given file
descriptor.

4
FileWriter(String fileName)

This constructor creates a FileWriter object, given a file name.

5
FileWriter(String fileName, boolean append)

This constructor creates a FileWriter object given a file name with a boolean
indicating whether or not to append the data written.

Once you have FileWriter object in hand, then there is a list of helper methods, which can be
used to manipulate the files.

https://www.tutorialspoint.com/java/java_filewriter_class.htm 2/5
8/9/23, 20:52 Java - FileWriter Class

Sr.No. Method & Description

1
public void write(int c) throws IOException

Writes a single character.

2
public void write(char [] c, int offset, int len)

Writes a portion of an array of characters starting from offset and with a length of
len.

3
public void write(String s, int offset, int len)

Write a portion of a String starting from offset and with a length of len.

Example
Following is an example to demonstrate class −

 Live Demo
import java.io.*;
public class FileRead {

public static void main(String args[])throws IOException {


File file = new File("Hello1.txt");

// creates the file


file.createNewFile();

https://www.tutorialspoint.com/java/java_filewriter_class.htm 3/5
8/9/23, 20:52 Java - FileWriter Class

// creates a FileWriter Object


FileWriter writer = new FileWriter(file);

// Writes the content to the file


writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();

// Creates a FileReader Object


FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array

for(char c : a)
System.out.print(c); // prints the characters one by one
fr.close();
}
}

This will produce the following result −

Output
This
is
an
example

https://www.tutorialspoint.com/java/java_filewriter_class.htm 4/5
8/9/23, 20:52 Java - FileWriter Class

 Print Page

https://www.tutorialspoint.com/java/java_filewriter_class.htm 5/5

You might also like