FileDocCategorySizeDatePackage
FileOutputStream.javaAPI DocAndroid 1.5 API11906Wed May 06 22:41:04 BST 2009java.io

FileOutputStream

public class FileOutputStream extends OutputStream implements Closeable
A specialized {@link OutputStream} that writes to a file in the file system. All write requests made by calling methods in this class are directly forwarded to the equivalent function of the underlying operating system. Since this may induce some performance penalty, in particular if many small write requests are made, a FileOutputStream is often wrapped by a BufferedOutputStream.
see
BufferedOutputStream
see
FileInputStream
since
Android 1.0

Fields Summary
FileDescriptor
fd
The FileDescriptor representing this FileOutputStream.
boolean
innerFD
private FileChannel
channel
private org.apache.harmony.luni.platform.IFileSystem
fileSystem
Constructors Summary
public FileOutputStream(File file)
Constructs a new FileOutputStream on the File {@code file}. If the file exists, it is overwritten.

param
file the file to which this stream writes.
throws
FileNotFoundException if {@code file} cannot be opened for writing.
throws
SecurityException if a {@code SecurityManager} is installed and it denies the write request.
since
Android 1.0


                                                                                                                         
         
        this(file, false);
    
public FileOutputStream(File file, boolean append)
Constructs a new FileOutputStream on the File {@code file}. The parameter {@code append} determines whether or not the file is opened and appended to or just opened and overwritten.

param
file the file to which this stream writes.
param
append indicates whether or not to append to an existing file.
throws
FileNotFoundException if the {@code file} cannot be opened for writing.
throws
SecurityException if a {@code SecurityManager} is installed and it denies the write request.
since
Android 1.0

        super();
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(file.getPath());
        }
        fd = new FileDescriptor();
        fd.descriptor = fileSystem.open(file.properPath(true),
                append ? IFileSystem.O_APPEND : IFileSystem.O_WRONLY);
        innerFD = true;
        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
                append ? IFileSystem.O_APPEND : IFileSystem.O_WRONLY);
    
public FileOutputStream(FileDescriptor fd)
Constructs a new FileOutputStream on the FileDescriptor {@code fd}. The file must already be open, therefore no {@code FileNotFoundException} will be thrown.

param
fd the FileDescriptor to which this stream writes.
throws
NullPointerException if {@code fd} is {@code null}.
throws
SecurityException if a {@code SecurityManager} is installed and it denies the write request.
since
Android 1.0

        super();
        if (fd == null) {
            throw new NullPointerException(Msg.getString("K006c")); //$NON-NLS-1$
        }
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(fd);
        }
        this.fd = fd;
        innerFD = false;
        channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
                IFileSystem.O_WRONLY);
    
public FileOutputStream(String filename)
Constructs a new FileOutputStream on the file named {@code filename}. If the file exists, it is overwritten. The {@code filename} may be absolute or relative to the system property {@code "user.dir"}.

param
filename the name of the file to which this stream writes.
throws
FileNotFoundException if the file cannot be opened for writing.
throws
SecurityException if a {@code SecurityManager} is installed and it denies the write request.
since
Android 1.0

        this(filename, false);
    
public FileOutputStream(String filename, boolean append)
Constructs a new FileOutputStream on the file named {@code filename}. The parameter {@code append} determines whether or not the file is opened and appended to or just opened and overwritten. The {@code filename} may be absolute or relative to the system property {@code "user.dir"}.

param
append indicates whether or not to append to an existing file.
param
filename the name of the file to which this stream writes.
throws
FileNotFoundException if the file cannot be opened for writing.
throws
SecurityException if a {@code SecurityManager} is installed and it denies the write request.
since
Android 1.0

        this(new File(filename), append);
    
Methods Summary
public voidclose()
Closes this stream. This implementation closes the underlying operating system resources allocated to represent this stream.

throws
IOException if an error occurs attempting to close this stream.
since
Android 1.0

        if (fd == null) {
            // if fd is null, then the underlying file is not opened, so nothing
            // to close
            return;
        }

        if (channel != null) {
            synchronized (channel) {
                if (channel.isOpen() && fd.descriptor >= 0) {
                    channel.close();
                }
            }
        }

        synchronized (this) {
            if (fd.descriptor >= 0 && innerFD) {
                fileSystem.close(fd.descriptor);
                fd.descriptor = -1;
            }
        }
    
protected voidfinalize()
Frees any resources allocated for this stream before it is garbage collected. This method is called from the Java Virtual Machine.

throws
IOException if an error occurs attempting to finalize this stream.
since
Android 1.0

        close();
    
public java.nio.channels.FileChannelgetChannel()
Returns the FileChannel equivalent to this output stream.

The file channel is write-only and has an initial position within the file that is the same as the current position of this stream within the file. All changes made to the underlying file descriptor state via the channel are visible by the output stream and vice versa.

return
the file channel representation for this stream.
since
Android 1.0

        return channel;
    
public final java.io.FileDescriptorgetFD()
Returns a FileDescriptor which represents the lowest level representation of an operating system stream resource.

return
a FileDescriptor representing this stream.
throws
IOException if an error occurs attempting to get the FileDescriptor of this stream.
since
Android 1.0

        return fd;
    
private synchronized voidopenCheck()

        if (fd.descriptor < 0) {
            throw new IOException();
        }
    
public voidwrite(byte[] buffer)
Writes the entire contents of the byte array {@code buffer} to this stream.

param
buffer the buffer to be written to the file.
throws
IOException if this stream is closed or an error occurs attempting to write to this stream.
since
Android 1.0

        write(buffer, 0, buffer.length);
    
public voidwrite(byte[] buffer, int offset, int count)
Writes {@code count} bytes from the byte array {@code buffer} starting at {@code offset} to this stream.

param
buffer the buffer to write to this stream.
param
offset the index of the first byte in {@code buffer} to write.
param
count the number of bytes from {@code buffer} to write.
throws
IndexOutOfBoundsException if {@code count < 0} or {@code offset < 0}, or if {@code count + offset} is greater than the length of {@code buffer}.
throws
IOException if this stream is closed or an error occurs attempting to write to this stream.
throws
NullPointerException if {@code buffer} is {@code null}.
since
Android 1.0

        // BEGIN android-changed
        // Exception priorities (in case of multiple errors) differ from
        // RI, but are spec-compliant.
        // removed redundant check, made implicit null check explicit,
        // used (offset | count) < 0 instead of (offset < 0) || (count < 0)
        // to safe one operation
        if (buffer == null) {
            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
        }
        if ((count | offset) < 0 || count > buffer.length - offset) {
            throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        // END android-changed

        if (count == 0) {
            return;
        }

        openCheck();
        fileSystem.write(fd.descriptor, buffer, offset, count);
    
public voidwrite(int oneByte)
Writes the specified byte {@code oneByte} to this stream. Only the low order byte of the integer {@code oneByte} is written.

param
oneByte the byte to be written.
throws
IOException if this stream is closed an error occurs attempting to write to this stream.
since
Android 1.0

        openCheck();
        byte[] byteArray = new byte[1];
        byteArray[0] = (byte) oneByte;
        fileSystem.write(fd.descriptor, byteArray, 0, 1);