Originally posted by Jose Campana:
I'd to like to know if there's a way to do it with the PrintWriter only, but apparently there's not.
No there isn't. You know why? Because even your solution that overwrites the contents uses a FileWriter (actually a FileOutputStream and OutputStreamWriter).
Here's the
Java 6 code of the constructor you're using:
In other words: the File is wrapped in a FileOutputStream, that is wrapped in an OutputStreamWriter, that is wrapped in a BufferedWriter.
Of course this could be shorter:
The PrintStream constructors that take a file or string are merely convenience constructor that were added in Java 5. Prior to this, you
had to use a FileWriter or FileOutputStream.