Taking It From The Stream
Introduction to File Input Operations
The process for inputting data from a file is not significantly different from outputting, but there are a couple of issues that need addressing. As you read previously, when you output to a file, the file you are outputting to is either created, or overwritten, when you open the file. The input file is not created by your program; it is accessed by your program. And if it is not available, there is a problem. Here is an example of data that might be in a text file, placed there by a program such as one of the examples in the previous topic.
16, 7.25, Bill
Now here is an example code segment that might retrieve the given data from the file.
// variable initialization
File_Input_Class fileIn = new File_Input_Class();
String name, fileName = "textfile.txt";
int age;
double payRate;
// open the file, test for success
// method: open
if( fileIn.open( fileName ) )
{
// input the data
// method: getInt, getDouble, getString
age = fileIn.getInt();
payRate = fileIn.getDouble();
name = fileIn.getString();
// close the file
// method: closeInputFile
fileIn.closeInputFile();
}
// otherwise, assume file access failure
else
{
// display error message
// method: println
System.out.println( "ERROR: file not found\n" );
}
First, notice the test for success in the input operation
Next, notice the use of System.out.println. When there are just simple things to output, you are welcome to use this Java tool as needed. At the same time, when your output gets a little more complicated, you can use the Console_IO_Class tools as needed.
The next item is a little more complicated. While the output process works every time without fail, the input process may not. As mentioned, if there is no file available, or no data in the file, attempting to upload data that doesn’t exist will cause problems for your program. So, the nice folks who designed the File_Input_Class built in a little extra help. If there is ever a failure in the openInputFile operation, the method will return false so you can handle that issue in your program. This is also true for the closeInputFile method but the possibility of failure there is much smaller. In addition to these benefits, none of the methods will attempt to acquire data if the file has not already been successfully opened, or if the end of file has been found, again saving your programs from inconvenient failures.
Now with all that said, you can review the code you were given. The program opens the file, it tests to see if the file was opened correctly and reports if it wasn’t, and then it attempts to acquire the data that should be in the file. Note that this is still not a guarantee that data will be acquired. This code segment just shows the input process. In some cases after the input process, you may still need to test the input file stream to see if there was any input failure before you attempt to acquire new data. While the File_Input_Class will protect you from attempting to acquire nonexistent data, there are some circumstances where may need to check and see if data is still available in the file. The checkForEndOfFile method in the File_Input_Class provides the ability to do this.
Important Specification Note
There is an important consideration related to the File_Input_Class that is provided in all the input operation specifications. In order to capture contiguous data, the "get" operations all have to reach for the next byte or numeric character in the file to see if it is part of the input data (e.g., integer, double, String, etc.). In every case, this means the operation will acquire the byte immediately after each input data item and ignore it (i.e, it will be lost). This should never be a problem since any given file data should never immediately follow any other given data item. However, if a comma or semicolon or other punctuation follows immediately after a given quantity of data, it will not be returned to the user. This is not a significant problem but as users of the FILE_INPUT_CLASS, you as the programmer should know about this.
The file input class can be found here.
Watch the video for a program implementing this process.