Java Programs  «Prev 

Communicating with standard I/O

Are they standard or not?

The keyboard and monitor are always the standard input and output devices used in standard I/O. Although this generally is the case, it is technically possible for a computer to rely on a different device for standard I/O. A good example is an embedded computer in an electronic device that does not have a monitor. In this case, the standard output device could be just about anything, maybe a series of lights or possibly even an audible alert. Another example is a data capture device that has no keyboard and receives all of its information from a live data feed.
Granted, both of these examples are a little out of the ordinary in terms of the types of Java programs you are likely to develop. However, Java is well on its way to becoming an embedded programming language that is used in all kinds of hardware such as cellular phones and video games. Also, do not assume that graphical Java programs are unaffected by standard I/O. Applets can output information to the standard output device using a special window within a Web browser called a console window.

Classical Java Streams Prior to Java 8

Most fundamental I/O in Java is based on streams. A stream represents a flow of data with (at least conceptually) a writer at one end and a reader at the other. When you are working with the java.io package to perform terminal input and output, reading or writing files, or communicating through sockets in Java, you are using various types of streams. The NIO package introduces a similar concept called a channel. One difference betwen the two is that streams are oriented around 1) bytes or 2) characters while channels are oriented around "buffers" containing those data types, yet they perform roughly the same job.

Java I/O System

1. Binary I/O streams (ascii, 8 bits)
  1. InputStream
  2. OutputStream
2. The decorator design pattern
3. Character I/O streams (Unicode, 16 bits)
  1. Reader
  2. Writer
4. Comparing Binary I/O to Character I/O
5. Files and directories : The class File
6. Object Serialization
Light-weight persistence