You have already used this
Up to this point, you have only captured data from the keyboard for your programs. However, programming can become quite a bit more interesting if you can acquire greater quantities of data with which to work, especially if you do not have to enter all the data each time you want to run the program. In this topic, you will learn how to access data from a text file, and how to place data in a text file. This will extend your ability to create larger and more interesting programs.
Data Streams
Up to this point, you have primarily been using utility methods for your console input and output (i.e., I/O). In the last group of topics related to iteration, you used the cin and cout methods as well as the extraction and insertion operators. Thanks to the Java programming language developers, these are fairly straightforward keyboard input and monitor output operations.
Consider the following examples.
// prompt the user, get the first digit
// method: promptForInt
firstDigit = conIO.promptForInt( "Enter the first digit: " );
// prompt the user, get the second digit
// method: promptForInt
secondDigit = conIO.promptForInt( "Enter the second digit: " );
// add the digits
sum = firstDigit + secondDigit;
// output the result
// method: printString, printEndline
conIO.printString( "The sum of these two digits is: " + sum );
conIO.printEndline();
The conIO object "connects" the keyboard to the program you are developing so when the person types the values into the keyboard, it follows the data stream through the operating system and into your specific program. Remember that the data stream is an abstraction representing all the hardware and software operations required to drive the input process. This is an excellent way to metaphorically understand the process without having to know everything about the "underground" stuff.
For the display part of the prompting action, and then for the display of the sum, you again have data in your program that must go out to the monitor. By declaring the conIO object in your specific program, it means that only your information will be displayed on only the monitor. Again, the data stream concept supports thinking about the "flow" of data without sweating the hardware and software details that are actually doing the business.
This stream abstraction, discussed in a previous chapter, is important for you to understand because it is also how data is acquired from other I/O devices, such as hard drives, removable drives (thumb drives, etc.), CD/DVD drives, and so on. And it is how data can be sent to output devices such as these drives, as well as others, such as printers, plotters, and other display devices.
The data stream concept for file I/O is identical to the one used for console input and output. The only difference is that for console I/O, you create the conIO object so you can use them. That works because for any given program there will only be one keyboard and one monitor involved. As a side note, there could be other displays but we won't worry about them at this point. Like the Console_IO_Class, you will be opening and naming your own file objects, but unlike the Console_IO_Class, you may actually create more than one object to open more than one file or other I/O stream at once. You will see more about that later.
Notice the word abstraction (again). You have already learned how to get data from a console stream (i.e., the keyboard), or to send data to a console stream (i.e., the monitor); and hard drives and removable disks are simply abstracted as, or acting like, other kinds of streams. As far as the computer is concerned, it is all the same.
As long as you understand that writing to a file or reading from a file is exactly the same process as sending data to a monitor or getting data from a keyboard, you will get along swimmingly. In the next topics, you will be looking at the syntax and operations for accessing the files but the actual act of outputting to a file or inputting from a file will look almost identical to the operations you have been using for a while.