Two Write A File
I learned how to print text in Java using System.out, so it was natural for me to use the PrintStream to write text out to a file. Here is how I used to write text to a file:
FileOutputStream fos = new FileOutputStream(filePathStr);
PrintStream ps = new PrintStream(fos);
ps.println("Hello, World!");
ps.close();
To write some quick debugging message to a file I would sometimes use the FileWriter as in the following example:
FileWriter fw = new FileWriter(filePathStr);
fw.write("Hello, World!");
fw.close();
Now with Java 1.5 I am exclusively using the PrintWriter. Unlike FileWriter, Printwriter defines and overloads all my favorite println methods that I am so used to by from the years of using the System.out PrintStream.
PrintWriter pw = new PrintWriter(filePathStr);
pw.println("Hello, World!");
pw.close();