Reputation: 11
is there an advantage to using the FileWriter() class & constructor instead of the createNewFile() method in the File class?
I can't figure out the difference or the advantage. createNewFile() seems simpler and more intuitive so I'm thinking of abandoning my use of the FileWriter but I wanted to check.
Thanks.
Upvotes: 1
Views: 1559
Reputation: 117587
If you just need to create an empty file, then use File.createNewFile()
. If you need to write to a file, use FileWriter
. The constructor FileWriter(String fileName, boolean append)
has append
boolean parameter which is useful in most cases.
However, I recommend to use PrintWriter
rather than using FileWriter
to write char-based data to a file, because of its useful methods such as println()
.
Upvotes: 0
Reputation: 38526
File.createNewFile() creates only an empty file. A FileWriter
additionally allows you to write character-based data out to a file.
Use the most direct and obvious method available. If you want an empty new file, the former will work fine. Anything more complicated will usually involve a Writer
or OutputStream
of some kind.
Upvotes: 6