Introduction To The Text File
As mentioned in the previous topic, the streams you were using previously were related to the computer console (i.e., the keyboard and monitor). You will now be working with file streams. In its simplest definition, file I/O is access to some device that has stored, or is able to store, data. This data can be in one of two forms. It is either in text form, which means the data output to the storage device actually looks just like it did when you outputted it to your monitor screen; or it is in binary form which is a series of ones and zeros representing the data. Generally speaking, binary files are more compact, but they can usually only be viewed by the program that stored them; this is almost always how data is stored in industrial-grade programs.
However, for educational purposes, you will be using text file storage, so that if you save something to a file, you will be able to view it with any editor such as the one on which you are presently developing your programs. Notepad is a small editor included with most Windows™ programs, and can also be used to view the results of your program file storage. This whole process is called file I/O, and you will be storing your data as text files.
There are two changes you will have to make to add file I/O to your repertoire. First, you will need to import the FileReader and the FileWriter classes and due to some exception-handling actions that are required of the input process, you will likely have to import a couple of exception classes. Second, and this is familiar to you, you will need to create objects of the FileReader or FileWriter classes as you conduct your file I/O operations. While there will be differences in the methods you call, the instantiation process is identical. Stay tuned.
Once you have learned to capture data from files as well as output data to files, you will be able to implement more powerful, and less trivial, programs for your Computer Science learning process. The following topics provide several examples of how this is done.