PrintWriter and PrintStream in Java

Difference between PrintWriter and PrintStream

First of all, the PrintWriter class uses an improved way of working with Unicode characters and a different output buffering mechanism: in the PrintStream class, the output buffer was flushed whenever the print() or println() method was called, and when using the PrintWriter class, there is an option to opt out of automatic flushing buffers, executing it explicitly using the flush() method.

In addition, the methods of the PrintWriter class never throw an exception. To check for errors, you must explicitly call the checkError() method.

What is the difference and what do InputStream, OutputStream, Reader, Writer have in common

  • InputStream and its heirs - a collection for receiving byte data from various sources.
  • OutputStream and its heirs - a set of classes that define stream byte output.
  • Reader and its descendants define streaming Unicode character input.
  • Writer and its descendants define streaming output of Unicode characters.

What classes allow to convert byte streams to character streams and vice versa?

  • OutputStreamWriter is the bridge between the OutputStream class and the Writer class. The characters written to the stream are converted to bytes.
  • InputStreamReader - analog for reading. Using the methods of the Reader class, bytes are read from the InputStream and then converted to characters.

Which classes allow to speed up read/write by using a buffer?

  • BufferedInputStream(InputStream in) / BufferedInputStream(InputStream in, int size)
  • BufferedOutputStream(OutputStream out) / BufferedOutputStream(OutputStream out, int size)
  • BufferedReader(Reader r) / BufferedReader(Reader in, int sz)
  • BufferedWriter(Writer out) / BufferedWriter(Writer out, int sz)

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed