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();

Related posts:

  1. XML Object Cereal
  2. Using Dom4J: Writing An XML
  3. The Word Is POI
  4. Class HighLite File
  5. Write a 3D Game in Java

This entry was posted in Java, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*