Question

If I can do this:

PrintWriter pw = new PrintWriter("file.txt");
pw.println("Hello");

Why PW is often use like this:

PrintWriter pw = new PrintWriter(new FileOutputStream("file.txt"));
pw.println("Hello");

What's the difference?

Was it helpful?

Solution

The PrintWriter constructors that accept a file name or a java.io.File are relatively new; they were added in JDK 1.5. The FileOutputStream or FileWriter version used to be the only alternative; a lot of people don't even know about the new constructors in PrintWriter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top