loader image
Skip to main content
If you continue browsing this website, you agree to our policies:
x

Topic outline

  • Unit 8: Java I/O and Exception Handling

    In this unit, we discuss two important programming concepts in Java: input and output. Input and output techniques allow programmers to connect the virtual world of computers to the real world. Because of this, you must fully understand how to use a programming language's built-in I/O (input/output) functionality. In this unit, we discuss function I/O before moving on to file I/O (writing to and reading data from files). Each unit contains a discussion of the applicable Java classes, which are part of the standard programming language (FileWriter, PrintWriter, FileReader, BufferedReader, IOException). We then identify the common pitfalls and design concepts that you should keep in mind as a programmer. By the end of this unit, you will have a strong understanding of how to write and read from a file and how to write a Java program that performs these functions.

    Completing this unit should take you approximately 6 hours.

    • Upon successful completion of this unit, you will be able to:

      • describe the Java I/O package;
      • read and write data from/to an external file;
      • use the Java I/O package to retrieve data for populating method parameters;
      • explain error-handling via exceptions; and
      • apply exception handling techniques.
    • 8.1: Input/Output in Java

      • Java provides a Scanner class to facilitate data input/output. In this section, you will learn about the Scanner class that is used to get input from the user. Java also defines various methods from the Scanner class that can convert user input into appropriate data types before conducting any operation on the data.

      • Complete this review exercise. Think of the best response to fill in the blanks for each question, and then click on the blank in each question to reveal the correct response.

      • Read this article to learn how to format your output for specific purposes.

      • Read this tutorial.

    • 8.2: Writing Data to a File

      • This chapter explains how input and output streams can be used for writing files, similar to how they are used for writing to the display.

      • This chapter explains Java's FileWriter class and how you can use it to store data in files.

      • Attempt this ungraded quiz.

    • 8.3: Reading Data from a File

      • This chapter discusses Java's FileReader and BufferedReader classes in detail. FileReader and BufferedReader classes are used together when reading data from an external file. The use of the BufferedReader class allows data to be buffered as it is read from a file before manipulating it. The readLine() method of the BufferedReader class reads a line of text from a character-oriented input stream, and puts it into a new String object.

      • Attempt this ungraded quiz.

    • 8.4: File Input for Method Arguments

      • Section 4.6 describes and illustrates how to get the values of method arguments from a file.

    • 8.5: Handling Exceptions

      • It is not a matter of IF but WHEN things will go wrong in a computer program. Sometimes there are bugs, errors of one form or another. There are also unforeseen use cases. You can never assume a computer program is perfect. Exception-Handling helps us to catch erroneous events and devise means of correcting them. We discuss this topic here since exception-handling can take more code than should be put into the main line of execution. In such cases, a method in an exception-handling class should be called. Exception handling mechanisms allow a program to continue executing, instead of terminating it abruptly, even if an error occurs in the program.

    • Unit 8 Assessment

      • Take this assessment to see how well you understood this unit.

        • This assessment does not count towards your grade. It is just for practice!
        • You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
        • You can take this assessment as many times as you want, whenever you want.