Reading Writing Text  «Prev  Next»


Lesson 15 The FileWriter class
ObjectiveWrite a program that copies text from one file to another.

Java FileWriter Class

The FileWriter class writes text files using the platform's default character encoding and buffer size. If you need to change these values, construct an OutputStreamReader on a FileOutputStream instead.
There are four FileWriter constructors.
Question: What is the best way to copy text from 1 file to another in Java?
public FileWriter(String fileName) throws IOException

public FileWriter(String fileName, boolean append) throws IOException

public FileWriter(File file) throws IOException

public FileWriter(FileDescriptor fd)


No methods other than the constructors are declared in this class. You use the standard Writer methods like write(), flush(), and close() to actually write the data in the file.

Reading Writing Java Files - Exercise

Click the Exercise link below to write a program that uses a FileReader and a FileWriter to copy text from the file named in the first command line argument to the file named in the second command line argument.
Reading Writing Java Files - Exercise