APIdia Launch Partner
Top Description Fields Constructors Methods
java.io

public Class FileOutputStream

extends OutputStream
Class Inheritance
Known Direct Subclasses
java.lang.Process.PipeOutputStream, java.lang.System.Out
Imports
java.nio.channels.FileChannel, jdk.internal.access.SharedSecrets, .JavaIOFileDescriptorAccess, jdk.internal.event.FileWriteEvent, sun.nio.ch.FileChannelImpl

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

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

API Note

The close method should be called to release resources used by this stream, either directly, or with the try-with-resources statement.

Implementation Specification

Subclasses are responsible for the cleanup of resources acquired by the subclass. Subclasses requiring that resource cleanup take place after a stream becomes unreachable should use java.lang.ref.Cleaner or some other mechanism.

Author
Arthur van Hoff
Since
1.0
See Also
java.io.File, java.io.FileDescriptor, java.io.FileInputStream, java.nio.file.Files#newOutputStream

Field Summary

Modifier and TypeField and Description
private volatile FileChannel
channel

The associated channel, initialized lazily.

private volatile boolean
private final Object
private final FileDescriptor
fd

The system dependent file descriptor.

private static final JavaIOFileDescriptorAccess
FD_ACCESS

Access to FileDescriptor internals.

private static boolean
jfrTracing

Flag set by jdk.internal.event.JFRTracing to indicate if file writes should be traced by JFR.

private final String
path

The path of the referenced file (null if the stream is created with a file descriptor)

Constructor Summary

AccessConstructor and Description
public
FileOutputStream(String
the system-dependent filename
name
)

Creates a file output stream to write to the file with the specified name.

public
FileOutputStream(String
the system-dependent file name
name
,
boolean
if true, then bytes will be written to the end of the file rather than the beginning
append
)

Creates a file output stream to write to the file with the specified name.

public
FileOutputStream(File
the file to be opened for writing.
file
)

Creates a file output stream to write to the file represented by the specified File object.

public
FileOutputStream(File
the file to be opened for writing.
file
,
boolean
if true, then bytes will be written to the end of the file rather than the beginning
append
)

Creates a file output stream to write to the file represented by the specified File object.

public
FileOutputStream(FileDescriptor
the file descriptor to be opened for writing
fdObj
)

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this file output stream and releases any system resources associated with this stream.

public FileChannel

Returns:

the file channel associated with this file output stream
getChannel
()

Returns the unique FileChannel object associated with this file output stream.

public final FileDescriptor

Returns:

the FileDescriptor object that represents the connection to the file in the file system being used by this FileOutputStream object.
getFD
()

Returns the file descriptor associated with this stream.

private static native void
private void
open(String
name of file to be opened
name
,
boolean
whether the file is to be opened in append mode
append
)

Opens a file, with the specified name, for overwriting or appending.

private native void
open0(String
name of file to be opened
name
,
boolean
whether the file is to be opened in append mode
append
)

Opens a file, with the specified name, for overwriting or appending.

private void
traceWrite(int b, boolean append)

private void
traceWriteBytes(byte[] b, int off, int len, boolean append)

private native void
write(int
the byte to be written.
b
,
boolean
true if the write operation first advances the position to the end of file
append
)

Writes the specified byte to this file output stream.

public void
write(int
the byte to be written.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this file output stream.

public void
write(byte[] b)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this file output stream.

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

Overrides java.io.OutputStream.write.

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

private native void
writeBytes(byte[]
the data to be written
b
,
int
the start offset in the data
off
,
int
the number of bytes that are written
len
,
boolean
true to first advance the position to the end of file
append
)

Writes a sub array as a sequence of bytes.

Inherited from java.io.OutputStream:
flushnullOutputStream

Field Detail

channelback to summary
private volatile FileChannel channel

The associated channel, initialized lazily.

closedback to summary
private volatile boolean closed
closeLockback to summary
private final Object closeLock
fdback to summary
private final FileDescriptor fd

The system dependent file descriptor.

FD_ACCESSback to summary
private static final JavaIOFileDescriptorAccess FD_ACCESS

Access to FileDescriptor internals.

jfrTracingback to summary
private static boolean jfrTracing

Flag set by jdk.internal.event.JFRTracing to indicate if file writes should be traced by JFR.

pathback to summary
private final String path

The path of the referenced file (null if the stream is created with a file descriptor)

Constructor Detail

FileOutputStreamback to summary
public FileOutputStream(String name) throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. If the file exists, it is truncated, otherwise a new file is created. Symbolic links are automatically redirected to the target of the link. A new FileDescriptor object is created to represent this file connection.

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.

Implementation Specification

Invoking this constructor with the parameter name is equivalent to invoking new FileOutputStream(name, false).

Parameters
name:String

the system-dependent filename

Exceptions
FileNotFoundException:
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
FileOutputStreamback to summary
public FileOutputStream(String name, boolean append) throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. If the file exists, it is truncated unless the second argument is true, in which case bytes will be written to the end of the file rather than the beginning. If the file does not exist, it is created. Symbolic links are automatically redirected to the target of the link. A new FileDescriptor object is created to represent this file connection.

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.

Parameters
name:String

the system-dependent file name

append:boolean

if true, then bytes will be written to the end of the file rather than the beginning

Exceptions
FileNotFoundException:
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.
Since
1.1
FileOutputStreamback to summary
public FileOutputStream(File file) throws FileNotFoundException

Creates a file output stream to write to the file represented by the specified File object. If the file exists, it is truncated, otherwise a new file is created. Symbolic links are automatically redirected to the target of the link. A new FileDescriptor object is created to represent this file connection.

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.

Parameters
file:File

the file to be opened for writing.

Exceptions
FileNotFoundException:
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
See Also
java.io.File#getPath()
FileOutputStreamback to summary
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 file exists, it is truncated unless the second argument is true, in which case bytes will be written to the end of the file rather than the beginning. If the file does not exist, it is created. Symbolic links are automatically redirected to the target of the link. A new FileDescriptor object is created to represent this file connection.

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.

Parameters
file:File

the file to be opened for writing.

append:boolean

if true, then bytes will be written to the end of the file rather than the beginning

Annotations
@SuppressWarnings:this-escape
Exceptions
FileNotFoundException:
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
Since
1.4
See Also
java.io.File#getPath()
FileOutputStreamback to summary
public FileOutputStream(FileDescriptor fdObj)

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

If fdObj is null then a NullPointerException is thrown.

This constructor does not throw an exception if fdObj is invalid. However, if the methods are invoked on the resulting stream to attempt I/O on the stream, an IOException is thrown.

Parameters
fdObj:FileDescriptor

the file descriptor to be opened for writing

Annotations
@SuppressWarnings:this-escape

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.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.

If this stream has an associated channel then the channel is closed as well.

API Note

Overriding close to perform cleanup actions is reliable only when called directly or when called by try-with-resources.

Implementation Specification

Subclasses requiring that resource cleanup take place after a stream becomes unreachable should use the java.lang.ref.Cleaner mechanism.

If this stream has an associated channel then this method will close the channel, which in turn will close this stream. Subclasses that override this method should be prepared to handle possible reentrant invocation.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getChannelback to summary
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.

Returns:FileChannel

the file channel associated with this file output stream

Since
1.4
getFDback to summary
public final FileDescriptor getFD() throws IOException

Returns the file descriptor associated with this stream.

Returns:FileDescriptor

the FileDescriptor object that represents the connection to the file in the file system being used by this FileOutputStream object.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FileDescriptor
initIDsback to summary
private static native void initIDs()
openback to summary
private void open(String name, boolean append) throws FileNotFoundException

Opens a file, with the specified name, for overwriting or appending.

Parameters
name:String

name of file to be opened

append:boolean

whether the file is to be opened in append mode

open0back to summary
private native void open0(String name, boolean append) throws FileNotFoundException

Opens a file, with the specified name, for overwriting or appending.

Parameters
name:String

name of file to be opened

append:boolean

whether the file is to be opened in append mode

traceWriteback to summary
private void traceWrite(int b, boolean append) throws IOException
traceWriteBytesback to summary
private void traceWriteBytes(byte[] b, int off, int len, boolean append) throws IOException
writeback to summary
private native void write(int b, boolean append) throws IOException

Writes the specified byte to this file output stream.

Parameters
b:int

the byte to be written.

append:boolean

true if the write operation first advances the position to the end of file

writeback to summary
public void write(int b) throws IOException

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this file output stream. Implements the write method of OutputStream.

Parameters
b:int

the byte to be written.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this file output stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.OutputStream.write.

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

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
IndexOutOfBoundsException:
If off is negative, len is negative, or len is greater than b.length - off
writeBytesback to summary
private native void writeBytes(byte[] b, int off, int len, boolean append) throws IOException

Writes a sub array as a sequence of bytes.

Parameters
b:byte[]

the data to be written

off:int

the start offset in the data

len:int

the number of bytes that are written

append:boolean

true to first advance the position to the end of file

Exceptions
IOException:
If an I/O error has occurred.