Completion requirements
This chapter explains Java's FileWriter class and how you can use it to store data in files.
8. FileWriter Constructors
Answer:
No
FileWriter Constructors
Here are two (out of several) FileWriter
constructors:
FileWriter(String fileName)
FileWriter(String fileName, boolean append)
The first constructor's argument fileName
is the name of a disk file that will be created in the current subdirectory. If there is already a file with that name, it will be replaced.
Here is an excerpt from the example program. The FileWriter
constructor creates a disk file, reaper.txt. The constructor returns a reference to the FileWriter
object which is
assigned to writer
. Now writer
can be used to write to that file.
public static void main ( String[] args ) throws IOException { String fileName = "reaper.txt" ; FileWriter writer = new FileWriter( fileName ); . . . |
The second form of the constructor has an argument,
append
. If append
is true
, the constructor will open an existing file for writing without destroying its contents. If the file does not exist, it will be created.Question 8:
What happens if the file name is not correct?