According to coderanch.com, if we combine the answers we get:
FileWriter is the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.
PrintWriter & FileWriter.
Similarities
- Both extend from Writer.
- Both are character representation classes, that means they work with characters and convert them to bytes using default charset.
Differences
- FileWriter throws IOException in case of any IO failure, this is a checked exception.
- None of the PrintWriter methods throw IOExceptions, instead they set a boolean flag which can be obtained using checkError().
- PrintWriter has an optional constructor you may use to enable auto-flushing when specific methods are called. No such option exists in FileWriter.
- When writing to files, FileWriter has an optional constructor which allows it to append to the existing file when the "write()" method is called.
Difference between PrintStream and OutputStream: Similar to the explanation above, just replace character with byte.
PrintWriter has following methods :
close()
flush()
format()
printf()
print()
println()
write()
and constructors are :
File (as of Java 5)
String (as of Java 5)
OutputStream
Writer
while FileWriter having following methods :
close()
flush()
write()
and constructors are :
File
String
Link: http://www.coderanch.com/t/418148/java-programmer-SCJP/certification/Information-PrintWriter-FileWriter
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…