SCJP Study Guide:
API Contents


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs Study Guide: SCJP: FileOutputStream

FileOutputStream


The FileOutputStream class represents a byte stream that writes data to a file. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

The file can be specified using a FileDescriptor, a File object, or a String that represents a pathname. All of the constructors can throw a SecurityException if the application does not have permission to write to the specified file.

  • public FileOutputStream(String name) throws FileNotFoundException creates an output file stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument.

A SecurityException is thrown if its checkWrite method denies write access to the file. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

  • public FileOutputStream(String name, boolean append) throws FileNotFoundException creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument.

A SecurityException is thrown if its checkWrite method denies write access to the file. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

  • public FileOutputStream(File file) throws FileNotFoundException creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.

A SecurityException is thrown if its checkWrite method denies write access to the file. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

  • public FileOutputStream(File file, boolean append) throws FileNotFoundException creates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.

A SecurityException is thrown if its checkWrite method denies write access to the file. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

  • public FileOutputStream(FileDescriptor fdObj) creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system. First, if there is a security manager, its checkWrite method is called with the file descriptor fdObj argument as its argument. A SecurityException is thrown if its checkWrite method denies write access to the file.

FileOutputStream provides a low-level interface for writing data to a file. Wrap a FileOutputStream with a DataOutputStream or a PrintStream if you need a higher-level interface that can handle writing strings and binary data. You should also think about wrapping a FileOutputStream with a BufferedOutputStream to increase writing efficiency.

  • public void write(int b) throws IOException Writes the specified byte to this file output stream. Implements the write method of OutputStream. An IOException is thrown if an I/O error occurs.
  • public void write(byte[] b) throws IOException writes b.length bytes from the specified byte array to this file output stream. If b is null, a NullPointerException is thrown. An IOException is thrown if an I/O error occurs.
  • public void write(byte[] b,int off, int len) throws IOException writes len bytes from the specified byte array starting at offset off to this file output stream. If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. An IOException is thrown if an I/O error occurs.

Data must be written sequentially to a FileOutputStream; you can either overwrite existing data or append data to the end of the file. If you need random access to file data, use the RandomAccessFile class instead.

Do not forget to close and release the resource you opened:

  • public void close() throws IOException closes this file output stream and releases any system resources associated with this stream. This file output stream may no longer be used for writing bytes. If this stream has an associated channel then the channel is closed as well. An IOException is thrown if an I/O error occurs.
  • protected void finalize() throws IOException cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream. An IOException is thrown if an I/O error occurs.

Misc Methods:

  • public final FileDescriptor getFD() throws IOException returns the file descriptor associated with this stream. Returns the FileDescriptor object that represents the connection to the file in the file system being used by this FileOutputStream object. An IOException is thrown if an I/O error occurs.
  • public FileChannel getChannel() returns the unique FileChannel object associated with this file output stream. The initial position of the returned channel will be equal to the number of bytes written to the file so far unless this stream is in append mode, in which case it will be equal to the size of the file. Writing bytes to this stream will increment the channel's position accordingly. Changing the channel's position, either explicitly or by writing, will change this stream's file position.

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter .  

 


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |