Paul Anilprem wrote:They don't have identical methods. Specifically, methods of PrintStream do not throw IOException.
Neither do methods of PrintWriter. The
constructors of both PrintStream and PrintWriter throw IOException, but the methods do not.
What's weird about both classes, though, is that while they extend OutputStream and Writer, you usually have no need to use most of the
methods of those classes - except close(), maybe a few others. But if you're using either PrintStream or PrintWriter, you want to use the various print() methods, not the various write() methods. Note that those write() methods are where PrintStream and PrintWriter are actually different - but there's no good reason to use them. I would say this is a design flaw.
Originally, the most important difference between PrintStream and PrintWriter was in the constructors. A PrintStream could connect to OutputStreams, and a PrintWriter could connect to a Writer. So you would choose one or the other depending on what you were connecting to. However, over time, this difference diminished, as they added additional constructors to both classes to make it easier to connect either to whatever underlying stream/writer/file you want to. Note that you can connect a PrintWriter to an OutputStream, but you can't connect a PrintStream to a Writer. So far.
At this point, PrintWriter does everything you might want, and I would just use that wherever you can - except for System.out, which is still a PrintStream. But usually, that difference will not matter.