FileOutputStream

A file output stream is an output stream for writing data to a <code>File</code> or to a <code>FileDescriptor</code>. 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 {@code 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.

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

@apiNote To release resources used by this stream {@link #close} should be called directly or by try-with-resources. Subclasses are responsible for the cleanup of resources acquired by the subclass. Subclasses that override {@link #finalize} in order to perform cleanup should be modified to use alternative cleanup mechanisms such as {@link java.lang.ref.Cleaner} and remove the overriding {@code finalize} method.

@implSpec If this FileOutputStream has been subclassed and the {@link #close} method has been overridden, the {@link #close} method will be called when the FileInputStream is unreachable. Otherwise, it is implementation specific how the resource cleanup described in {@link #close} is performed.

@author Arthur van Hoff @see java.io.File @see java.io.FileDescriptor @see java.io.FileInputStream @see java.nio.file.Files#newOutputStream

Constructors

this
this(string name)

Creates a file output stream to write to the file with the specified name. A new <code>FileDescriptor</code> object is created to represent this file connection. <p> First, if there is a security manager, its <code>checkWrite</code> method is called with <code>name</code> as its argument. <p> 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 <code>FileNotFoundException</code> is thrown.

this
this(string name, bool append)

Creates a file output stream to write to the file with the specified name. If the second argument is <code>true</code>, then bytes will be written to the end of the file rather than the beginning. A new <code>FileDescriptor</code> object is created to represent this file connection. <p> First, if there is a security manager, its <code>checkWrite</code> method is called with <code>name</code> as its argument. <p> 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 <code>FileNotFoundException</code> is thrown.

this
this(File file)

Creates a file output stream to write to the file represented by the specified <code>File</code> object. A new <code>FileDescriptor</code> object is created to represent this file connection. <p> First, if there is a security manager, its <code>checkWrite</code> method is called with the path represented by the <code>file</code> argument as its argument. <p> 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 <code>FileNotFoundException</code> is thrown.

Members

Functions

close
void close()

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.

write
void write(byte[] b)

Writes <code>b.length</code> bytes from the specified byte array to this file output stream.

write
void write(int b)

Writes the specified byte to this file output stream. Implements the <code>write</code> method of <code>OutputStream</code>.

write
void write(byte[] b, int off, int len)

Writes <code>len</code> bytes from the specified byte array starting at offset <code>off</code> to this file output stream.

Inherited Members

From OutputStream

write
void write(int b)

Writes the specified byte to this output stream. The general contract for <code>write</code> is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument <code>b</code>. The 24 high-order bits of <code>b</code> are ignored. <p> Subclasses of <code>OutputStream</code> must provide an implementation for this method.

write
void write(string b)
Undocumented in source. Be warned that the author may not have intended to support it.
write
void write(byte[] b)

Writes <code>b.length</code> bytes from the specified byte array to this output stream. The general contract for <code>write(b)</code> is that it should have exactly the same effect as the call <code>write(b, 0, b.length)</code>.

write
void write(byte[] b, int off, int len)

Writes <code>len</code> bytes from the specified byte array starting at offset <code>off</code> to this output stream. The general contract for <code>write(b, off, len)</code> is that some of the bytes in the array <code>b</code> are written to the output stream in order; element <code>boff</code> is the first byte written and <code>b[off+len-1]</code> is the last byte written by this operation. <p> The <code>write</code> method of <code>OutputStream</code> calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation. <p> If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown. <p> If <code>off</code> is negative, or <code>len</code> is negative, or <code>off+len</code> is greater than the length of the array <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.

flush
void flush()

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of <code>flush</code> is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination. <p> If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive. <p> The <code>flush</code> method of <code>OutputStream</code> does nothing.

close
void close()

Closes this output stream and releases any system resources associated with this stream. The general contract of <code>close</code> is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened. <p> The <code>close</code> method of <code>OutputStream</code> does nothing.

Meta