Are PrintWriter and FileWriter in Java the same and no matter which one to use? So far I have used both because their results are the same. Is there some special cases where it makes sense to prefer one over the other?
public static void main(String[] args) { File fpw = new File("printwriter.txt"); File fwp = new File("filewriter.txt"); try { PrintWriter pw = new PrintWriter(fpw); FileWriter fw = new FileWriter(fwp); pw.write("printwriter text\r\n"); fw.write("filewriter text\r\n"); pw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
PrintWriter gives you some handy methods for formatting like println and printf . So if you need to write printed text - you can use it. FileWriter is more like "low-level" writer that gives you ability to write only strings and char arrays.
The biggest difference is that the print and println methods of PrintWriter take arguments of any type, generally calling the toString() or String. valueOf() methods to get String objects. The BufferedWriter write() method takes a single character, an array of characters, or a String.
public class PrintWriter extends Writer. Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream . It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
PrintStream and PrintWriter have nearly identical methods. The primary difference is that PrintStream writes raw bytes in the machine's native character format, and PrintWriter converts bytes to recognized encoding schemes.
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
Differences
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
Both of these use a FileOutputStream
internally:
public PrintWriter(File file) throws FileNotFoundException { this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))), false); } public FileWriter(File file) throws IOException { super(new FileOutputStream(file)); }
but the main difference is that PrintWriter offers special methods:
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With