CS101 Study Guide

Unit 8: Java I/O and Exception Handling

8a. Describe the Java I/O package

  • What package does Java use to move data into and out from a computer program?
  • What are the three fundamental data streams?
  • How is it possible to read data as one type and store it as another type?
  • How does a programmer communicate with the console?

We have spent a lot of time talking about organizing and manipulating data within the computer. But, we have spent little time on how to get data into and out of the computer. Java's utility package contains all we need for that. For this learning outcome we concentrate on the three fundamental data streams: System.in (data input), System.out (data output), System.err (error reporting). The computer has no value unless it has a positive impact on human needs. Input/Output allows the computer to connect to the real world.

What is "data" within the physical reality of the computer? Data exists as units of eight bits, switches that are either on or off. These units are called bytes. Data is composed of groupings and interpretations of bytes. If one takes a group of bytes and interprets that group differently, it becomes a different data value, or data values. Yes, it is possible that a single data value can be turned into multiple values. Turning one fundamental value into another is called casting. For instance,  double thisDouble = (double)3; turns the integer value 3 into the double value 3.0. Although not the same as casting, it is possible to turn a character string into a sequence of fundamental data values.

Data input and output is a vast subject within any language, depending on the type of data and where it comes from. Let's begin our exploration with the keyboard and console, two very common I/O devices.

Review:


8b. Read and write data from/to an external file

  • Name various sources of data that can be input to a computer program.
  • Name various targets of data output from a computer program.
  • What are the various ways to open a file?
  • What is good practice regarding the closing of files?

We have input data from the keyboard and output data to the console. There are many other sources and targets. These include files of different kinds, and also network addresses. "Data" itself has to be understood in the most general terms. A huge consideration is how the data is to be interpreted. Much depends on the source and target, and any intervening applications along the way.

In this particular learning exercise, we concentrate on file input/output. However, the lessons learned here are applicable to other sources and targets, as you will see as you learn more.

Review:


8c. Use the Java I/O package to retrieve data for populating method parameters

  • How is the Scanner class used for both keyboard and file input?
  • Why is it important to be able to read data from permanent storage devices?
  • What is the difference between a text and binary file?
  • Which other classes can be used for file input?

There are two general ways of dealing with data, as it arrives and historically. For instance, an application in precision irrigation will be interested in the immediate ambient temperature. However, that same application will be interested in temperature trends. For that reason, the immediate temperature has to be combined with past temperature readings. Thus, the immediate temperature has to be stored somehow for later access. How to read stored data values is what we consider in this component of the course.

Java offers several ways to read stored data. Here we consider the Scanner class combined with the File class. Storing and reading data is a vast subject within Java. Scanner and File classes offer basic approaches that fit well with numerous requirements. Let's begin our exploration of Java files with those.

Review java.io.File and File Input: There are two short readings here. Both should be taken in detail.


8d. Explain error-handling via exceptions

  • Under which type of situations are exceptions useful?
  • What do exception handlers do? What do they prevent?
  • What is the difference between error-checking and exception-handling?
  • Is it possible for a programmer to define their own exceptions? If so, how?

It is not a matter of if but when things will go wrong in a computer program. I began my consulting career by approaching a potential customer to tell them that he had seen the source code to their main multi-user system and that they were sure to have multiple crashes, periods of time when their system would go down and would have to be restarted. They replied that such a thing was not possible, that their users did not know enough to accomplish that. After asserting that it was because their users did not know enough that the system would crash, given the software at hand, I waited a week. At the end of that week, the customer received another visit. They begged for help since their system was spending more time crashing/restarting than getting anything useful accomplished. The fix was to insert easily-accomplished exception-handling into the code. The job was done in one morning of work. Such is the importance of exception-handling.

Taken in the most general sense, "exception" refers to an event that is unacceptable to the situational domain covered by the program. Anything outside that domain is an "exception". For instance, if the user is asked to input a value between 1 and 10 inclusive, steps must be taken to account for values outside that range. Similarly, if a file is supposed to exist, it is an "exception" when that file does not exist, or if the file contains unexpected data. Do not think this means you have to deal with an infinity of specific error situations. Rather, think in terms of, "Is this an expected situation?" If it is not, then an exceptional situation exists and must be declared and dealt with in some way that allows the program to keep operating. Given the relatively narrow range of "acceptable", it is easy to identify the "unacceptable". Philosophically, the question is, "Is this situation within the domain of acceptability?" If it is not, then an exception exists.

Review Exceptions: When Things Go Wrong: 10.1, 10.2, 10.3


8e. Apply exception handling techniques

  • What are the two constructs for identifying and handling exceptional (error) situations?
  • How can programs be best designed for smoothly identifying and dealing with exceptional situations?
  • Can a programmer create their own Java exceptions? If so, how?
  • Explain exceptions thrown by the JVM.

if/else and try/catch are the two approaches we can use to identify and deal with situations that are outside a program's domain. try/catch is important since it will allow the program to cope with errors generated by the JVM. For instance, if the program attempts to open a file that does not exist, the JVM will throw an exception. If the program does not check for JVM-thrown exceptions during the file-open attempt, the program will crash upon exception. try/catch is like saying, "if-exception then an error preventing continuation has occurred". If the exception is not caught and properly handled, the program crashes.

Continuing with that thought, if the file is not found, what should happen? Should the program store all internal data, close all open files, and then exit? Should the program go into a quiescent mode and try again later? Should the program go off to do something else for a while? Should it ask for a different filename? That depends on what the program is supposed to accomplish and the system environment in which it exists. How you design your software is an important undertaking that will make all the difference in maintenance, expansion, and reliability.

Review Exceptions: When Things Go Wrong: 10.4, 10.5, 10.6, 10.7


Unit 8 Vocabulary

This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.

  • casting
  • console
  • data
  • data streams
  • exception
  • exception-handling
  • File class
  • files
  • keyboard
  • Scanner class
  • utility package