WriteToFile.java

What the program does :

The small program creates a file and proceeds to write 100 random integer numbers into the file, 1 per line and then closes the file and exits.

How it does it :

Using the Printstream package in Java, the program creates a file object, and passes a name to it to create and open the file “randInts.txt” for input. A variable is declared using javas Random() method, and another variable sets the limit for the number of values written to the file.

Next, a FOR loop keeps writing integer values to the file, one at a time.

Once the limit in the FOR loop has been reached, the file closes, and the program exits.

The block of program statements is contained inside a Try … Catch block which is monitoring for file errors during program execution and will throw an exception if an error arises, resulting in an error message being written to the console.