Next: The FileInputStream class. Up: Files. Previous: The File class.
Textbook: Section 14.3
A FileOutputStream object represents a file into which you can write bytes. It's for writing raw bytes - we'll get into more sophisticated objects for writing characters or writing more complex items later.
When you create a FileOutputStream object, the file is opened for writing. The file is set up so that the first write occurs at the first byte of the file - thus, effectively, when you create a FileOutputStream object, the file it represents is immediately cleared.
A FileNotFoundException is an inappropriate name for what this method does, since if the file doesn't exist, it goes ahead and creates one. This exception is raised when creating the file is impossible - for example, if the file exists, but you don't have write permission for it. Or if it doesn't exist, and you don't have permission to create a new file in the directory.
As you write into the file, the FileOutputStream object tracks where you currently are in the file. This is called the file pointer.
Additionally, operating systems typically restrict each program on how many files they can have open at once. So if you don't close it, but your program tries to open several files later on, you may find that on the 64th file the program is suddenly no longer saving.
Next: The FileInputStream class. Up: Files. Previous: The File class.