Section 11d

File Output with Loops


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Supercharging File Operations

Reference Note: In the previous section, you learned about using the FileWriter class. It is not terribly complicated but it does have a few requirements that make it less than optimum for learning about file output. Therefore, as mentioned in the previous section, this reference will use a File_Output_Class that manages the process with much less complexity. In addition to learning about file output with loops, you will also have an opportunity to learn about this new class.

The file operations provided previously in this topic will be helpful for you to use when you are first trying out file operations. However, it gets much more interesting when you are outputting large quantities of data. For example, if you wanted to output a series of random numbers, you would probably write a method like the following.

public static void sendRandsToFile( String fileName, int numRands )
{
// initialize variables
int counter, nextVal;
File_Output_Class fileOut = new File_Output_Class();

// open file
// method: openOutputFile
fileOut.openOutputFile( fileName );

// loop up to number of random values requested
for( counter = 1; counter <= numRands; counter++ )
{
// get the next random value
// method: getRandBetween
nextVal = getRandBetween( LOW_RAND_VAL, HIGH_RAND_VAL );

// output random value
// method: writeInt
fileOut.writeInt( nextVal );

// check for needing a comma after all values but the last one
if( counter != numRands )
{
// send comma/space to file
// method: writeString
fileOut.writeString( COMMA_SPACE );
}

// check for end of row of values
if( counter % ITEMS_PER_ROW == 0 )
{
// send two end lines to file
// method: writeEndlines
fileOut.writeEndlines( TWO_ENDLINES );
}
}
// end loop

// add one more end of line
// method: writeEndline
fileOut.writeEndline();

// flush and close the file
// method: closeOutputFile
fileOut.closeOutputFile();
}

Watch this video to see the whole program that includes this method. As a bonus, the program also demonstrates the use of randomly generated integers, and how the random generation process is set up and implemented in a program.

The above method would open a file for output, load the file with the specified number of random values, and then close the file. For the condition: LOW_RAND_VAL = 10, HIGH_RAND_VAL = 99, ITEMS_PER_ROW = 5, and numRands = 17, the following might be found in the text file to which this data was sent.

65, 42, 19, 55, 57,

22, 14, 98, 33, 15,

27, 11, 44, 13, 18,

85, 93

It is important to remember that the output operation opens a file and puts data into it. It won’t concern itself over whether a file was there or not. If a file of the same name is in the same directory, this program will overwrite it without any indication or error message. This makes the file output actions pretty easy. You open it, you send data to it, and you close it. You don’t have to do any testing and you don’t have to have had a file already in place.

This is just one example of how you can output data to a file, also known as outputting, writing, or downloading to a file. There will be many more opportunities to output quantities of data using loops so be prepared to manage these. You can see how the commas are managed so that a comma does not appear before the first number or after the last number, and you can also see how the rows are limited to 5 items per row. These and the outputting process are all important, but again, no part of the outputting process is very complicated.

The file output class can be found here.

Your next step is to start thinking about getting data back from a file, called file input. You will find this discussion in the next topics.