Aikanáro
Aikanáro

Reputation: 849

Why PrintWriter is often use with FileOutputStream when I just can use the print method

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?

Upvotes: 3

Views: 3267

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

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.

Upvotes: 3

Related Questions