This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 11 Files and Networking C omputer programs are only useful if they interact with the rest of the world in some way. This interaction is referred to as input/output , or I/O . Up until now, this book has concentrated on just one type of interaction: interaction with the user, through either a graph- ical user interface or a command-line interface. But the user is only one possible source of information and only one possible destination for information. We have already encountered one other type of input/output, since TextIO can read data from files and write data to files. However, Java has an input/output framework that provides much more power and flexibility than does TextIO , and that covers other kinds of I/O in addition to files. Most importantly, it supports communication over network connections. In Java, input/output involving files and networks is based on streams , which are objects that support I/O commands that are similar to those that you have already used. In fact, standard output ( System.out ) and standard input ( System.in ) are examples of streams. Working with files and networks requires familiarity with exceptions, which were covered in Chapter 8. Many of the subroutines that are used can throw exceptions that require mandatory exception handling. This generally means calling the subroutine in a try..catch statement that can deal with the exception if one occurs. 11.1 Streams, Readers, and Writers W ithout the ability to interact with the rest of the world, a program would be useless. The interaction of a program with the rest of the world is referred to as input/output or I/O. Historically, one of the hardest parts of programming language design has been coming up with good facilities for doing input and output. A computer can be connected to many different types of input and output devices. If a programming language had to deal with each type of device as a special case, the complexity would be overwhelming. One of the major achievements in the history of programming has been to come up with good abstractions for representing I/O devices. In Java, the main I/O abstractions are called streams . Other I/O abstractions, such as “files” and “channels” also exist, but in this section we will look only at streams. Every stream represents either a source of input or a destination to which output can be sent. 11.1.1 Character and Byte Streams When dealing with input/output, you have to keep in mind that there are two broad categories of data: machine-formatted data and human-readable data. Machine-formatted data is repre- sented in binary form, the same way that data is represented inside the computer, that is, as 537 538 CHAPTER 11. FILES AND NETWORKING strings of zeros and ones. Human-readable data is in the form of characters. When you read a number such as 3.141592654 , you are reading a sequence of characters and interpreting them as a number. The same number would be represented in the computer as a bit-string that youas a number....
View Full Document
- Spring '11