Writing to console with System.out and PrintWriter

后端 未结 6 1912
独厮守ぢ
独厮守ぢ 2020-12-29 02:32

While reading about Java I/O, i realized that there are two ways through which i can write to the standard output.

Following is the snippet that uses both the techn

6条回答
  •  情深已故
    2020-12-29 03:02

    different of two approach is:

    • When you use System.out.print("") you actually used a PrintStream instance.
    • Actually the difference is in two classes PrintStream and PrintWriter which are:

      1. PrintStream is a stream of bytes while PrintWrite is a stream of characters.
      2. PrintStream uses platform's default encoding while with the PrintWriter you can however pass an OutputStreamWriter with a specific encoding. for sample: PrintStream stream = new PrintStream(output); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"));

    You can select a approach with your requirements.

提交回复
热议问题