Byte Streams and Conversion


Java represents standard input (stdin) and standard output (stdout) with stream objects stored in System.in and System.out, respectively. System.in refers to an InputStream, while System.out refers to a PrintStream. A PrintStream is a specialized OutputStream that simplifies writing objects of various types.

Both System.in and System.out are byte streams, not character streams. Remember that Java uses the multibyte character set Unicode, while most operating systems use a single-byte character set.

The java.io package provides a means of converting between byte streams and character streams. The class InputStreamReader wraps an InputStream and converts each byte read into an appropriate character. The conversion uses your platform system's default encoding scheme (as understood by Java) by default; you may also supply a decoding class that defines the mapping. In a similar fashion, the class OutputStreamWriter wraps an output stream, converting each character to a single byte.

The predominant use for InputStreamReader and OutputStreamWriter is to map between Java's character streams and stdin/stdout. Reader and Writer subclasses allow you to work with input and output on a line-by-line basis.

Do not send messages directly to System.in and System.out.


Referring to Reader and Writer abstract classes instead allows for quick redirecting to a different medium (e.g., files), and also makes for easier testing.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net