Java Tutorial - Java Script :
A file output stream can be created with the FileOutputStream(String) constructor. The usage is the same as the FileInputStream(String) constructor, so you can specify a path along with a filename. You have to be careful when specifying the file associated with an output stream. If it’s the same as an existing file, the original will be wiped out when you start writing data to the stream. You can create a file output stream that appends data after the end of an existing file with the FileOutputStream(String, boolean) constructor. The string specifies the file, and the Boolean argument should equal true to append data instead of overwriting existing data.
The file output stream’s write(int) method is used to write bytes to the stream. After the last byte has been written to the file, the stream’s close() method closes the stream. To write more than one byte, the write(byte[], int, int) method can be used. This works in a manner similar to the read(byte[], int, int) method described previously. The arguments to this method are the byte array containing the bytes to output, the starting point in the array, and the number of bytes to write. The ByteWriter application in Listing 15.2 writes an integer array to a file output stream.
The following things are taking place in this program:
· Lines 5–10—Create an integer array called data with 66 elements
· Lines 12 and 13—Create a file output stream with the filename pic.gif in the same folder as the ByteWriter.class file
· Lines 14 and 15—Use a for loop to cycle through the data array and write each element to the file stream Line 16—Closes the file output stream
After you run this program, you can display the pic.gif file in any web browser or graphics-editing tool. It’s a small image file in the GIF format