FileWriter

Java FileWriter Example

In this example, we will discuss the Java FileWriter class and how to use it to write streams of characters. This is the class used in Java in order to write to file.

Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. In such situations, the constructors in this class will fail if the file involved is already open.

FileWriter extends OutputStreamWriter, which is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.

The FileWriter class exists since JDK1.1.

1. The structure of the Java FileWriter

Constructor:

  • FileWriter(File file)Constructs a FileWriter object given a File object.
  • FileWriter(File file, boolean append)Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
  • FileWriter(FileDescriptor fd)Constructs a FileWriter object associated with a file descriptor.
  • FileWriter(String fileName)Constructs a FileWriter object given a file name.
  • FileWriter(String fileName, boolean append)Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

2. Overwriting vs. Appending the File

The constructor FileWriter(File file, boolean append) has a boolean flag as a second parameter and work as follows:

  • Overwrite

If append is set to false then it wil create a FileWriter that will overwrite any content that is currently in that file

  • Append

If append is set to true then it wil create a FileWriter that appends to an existing file.

3. The FileWriter in Java

To show a basic usage of FileWriter I created this class called SimpleFileWriterExample:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.javacodegeeks.examples;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class SimpleFileWriterExample {
    public static void main(String[] args) {
        String name = "John Doe";
        int age = 44;
        double temp = 26.9;
        FileWriter fw;
        try {
            fw = new FileWriter(new File("mytextfile.txt"));
             
            fw.write(String.format("My name is %s.",name));
            fw.write(System.lineSeparator()); //new line
            fw.write(String.format("I am %d years old.",age));
            fw.write(System.lineSeparator()); //new line
            fw.write(String.format("Today's temperature is %.2f.",temp));
            fw.write(System.lineSeparator()); //new line
            fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
         
        System.out.println("Done");
         
    }
 
}

What I just did was creating an instance of FileWriter, passing a File as a parameter, and then I started writing on it, using System.lineSeparator() to create new lines into the file. In the end of this, I closed the FileWriter instance, using the Sign up

Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button