2

PrintStream API says:

All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.

This SO answer states:

If an OutputStream deals with bytes, what about PrintStream.print(String)? It converts chars to bytes using the default platform encoding. Using the default encoding is generally a bad thing** since it can lead to bugs when moving from one platform to another, especially if you are generating the file on one platform and consuming it on another.

With a Writer, you typically specify the encoding to use, avoiding any platform dependencies.

Does't PrintStream(String fileName, String csn) use encoding to convert chars to bytes?

Why not use PrintStream constructors with encoding? We have same constructor API for PrintStream and PrintWriter! Why do we have this difference: "typically we don't provide encoding in PrintStream, and typically we do provide encoding in PrintWriter"?

1) But why we have encoding in PrintStream constructors?

PrintStream(File file, String csn)
PrintStream(String fileName, String csn)
PrintStream(OutputStream out, boolean autoFlush, String encoding)

If we use above constructors with encoding, does said in the beginning apply?

Besides, PrintWriter has exactly same constructors as PrintStream, so using PrintWriter(File file), PrintWriter(Writer out), PrintWriter(OutputStream out) is no better than using PrintStream constructors without specifying encoding?

I don't understand why people say PrintStream is unsafe because it uses default encoding and PrintWriter is better. I think that both constructors of PrintStream and PrintWriter without encoding are bad, and both constructors of PrintStream and PrintWriter with encoding is fine.

As I understand, PrintStream may get char, char[], String to its print/println methods and then it needs to convert chars to byte to output them into FileOutputStream or some other OutputStream given to its constructor. For this PrintStream creates OutputStreamWriter internally and gives encoding to its constructor (if encoding was given to PrintStream constructor).

caasdads
  • 409
  • 4
  • 12
  • 1
    The answer is in the question you linked. This is no longer true, `PrintStream` has support for encodings since `1.4`. The javadoc is wrong, probably because `PrintStream` is an old nigh-deprecated class that nobody cares about. – Kayaman May 29 '18 at 08:10
  • Am I right that the situation is the other way round: PrintWriter constructors from OutputStream never allow to specify encoding, while PrintStream constructors from OutputStream do allow to specify encoding. Concerning constructors from File - both PrintWriter and PrintStream allow encoding. – caasdads May 29 '18 at 08:16
  • 1
    Yes, `PrintWriter` is missing a constructor that would take `OutputStream` and an encoding. Now that you've noticed how bad both of those classes are, you know to stay away from them both. – Kayaman May 29 '18 at 08:19
  • May I ask just for one more word please? What shall be used instead of PrintStream and PrintWriter? CharBuffer and other subclasses of java.nio.Buffer? – caasdads May 29 '18 at 08:32
  • 2
    I have no idea how you made a jump from streams and writers to `Buffer`. They're not related in any way. But it depends on what you need. If you want to print text to the console, `System.out` is already a `PrintStream`, if you want to print text to file, there's `FileWriter`. There are hundreds of classes in the JDK, you have several ways to do things even if you're told "don't use class X". – Kayaman May 29 '18 at 08:35

0 Answers0