Working with basic I/O operations
We’ll use I/O operations to illustrate how exceptions work. Therefore, before diving into exception handling, we’ll briefly introduce basic I/O operations. There are many ways to do this, but we’ll be using FileReader
and FileWriter - FileReader
and FileWriter
are classes in the java.io
package that allow you to read and write characters. We have chosen these two classes because they provide a simple way to work with text files in Java and are commonly used for file I/O operations in the real world as well. First things first, let’s read with FileReader
.
Other classes for I/O operations
It is common to use other classes for I/O operations in common situations. For example, if you’re going to read lines from files, you may want to work with BufferedReader
instead. This is not the focus of this chapter. We just want to understand enough of I/O operations to demonstrate some real situations for exception handling...