Why need PrintWriter?

前端 未结 3 1555
梦谈多话
梦谈多话 2021-02-06 07:40

I am really confused about the purpose of various io classes, for example, If we have BufferedWriter, why we need a PrintWriter?

BufferedReader reader = new Buff         


        
3条回答
  •  情深已故
    2021-02-06 07:44

    The PrintWriter is essentially a convenience class. If you want to quickly and easily blast out a line of text to e.g. a log file, PrintWriter makes it very easy.

    Three features:

    • The print and println methods will take any data type and do the conversion for you. Not just String.
    • The relatively new format method is worth its weight in gold. Now it's as simple in Java as in C to output a line of text with C-style format control.
    • The methods never throw an exception! Some programmers are horrified at the possibility of never hearing about things going wrong. But if it's a throwaway program or doing something really simple, the convenience can be nice. Especially if output is to System.out or System.err which have few ways of going wrong.

提交回复
热议问题