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. or some other mechanism.
java.io.File , java.io.FileDescriptor , java.io.FileInputStream , java.nio.file.Files#newOutputStream
| Modifier and Type | Field 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) |
| Access | Constructor 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 append)true, then bytes will be written
to the end of the file rather than the beginningCreates 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 |
| public | FileOutputStream(File
the file to be opened for writing. file, boolean if append)true, then bytes will be written
to the end of the file rather than the beginningCreates a file output stream to write to the file represented by
the specified |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. 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 streamReturns the unique |
| public final FileDescriptor | Returns: theFileDescriptor object that represents
the connection to the file in the file system being used
by this FileOutputStream object.Returns the file descriptor associated with this stream. |
| private static native void | |
| private void | |
| private native void | |
| private void | |
| private void | |
| 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 fileWrites the specified byte to this file output stream. |
| public void | write(int
the byte to be written. b)Implements abstract java. Writes the specified byte to this file output stream. |
| public void | write(byte[] b)
Overrides java. Writes |
| public void | write(byte[] b, int off, int len)
Overrides java. Writes |
| 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 fileWrites a sub array as a sequence of bytes. |
| channel | back to summary |
|---|---|
| private volatile FileChannel channel The associated channel, initialized lazily. | |
| closed | back to summary |
|---|---|
| private volatile boolean closed | |
| closeLock | back to summary |
|---|---|
| private final Object closeLock | |
| fd | back to summary |
|---|---|
| private final FileDescriptor fd The system dependent file descriptor. | |
| FD_ACCESS | back to summary |
|---|---|
| private static final JavaIOFileDescriptorAccess FD_ACCESS Access to FileDescriptor internals. | |
| jfrTracing | back to summary |
|---|---|
| private static boolean jfrTracing Flag set by jdk.internal.event.JFRTracing to indicate if file writes should be traced by JFR. | |
| path | back to summary |
|---|---|
| private final String path The path of the referenced file (null if the stream is created with a file descriptor) | |
| FileOutputStream | back 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
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 Implementation Specification Invoking this constructor with the parameter
| |
| FileOutputStream | back 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
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
| |
| FileOutputStream | back to summary |
|---|---|
| public FileOutputStream(File file) throws FileNotFoundException Creates a file output stream to write to the file represented by
the specified
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
| |
| FileOutputStream | back to summary |
|---|---|
| public FileOutputStream(File file, boolean append) throws FileNotFoundException Creates a file output stream to write to the file represented by
the specified
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
| |
| FileOutputStream | back 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
This constructor does not throw an exception if
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. 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 Implementation Specification Subclasses requiring that resource cleanup take place after a stream becomes
unreachable should use the 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.
| |
| getChannel | back to summary |
|---|---|
| public FileChannel getChannel() Returns the unique The initial
| |
| getFD | back to summary |
|---|---|
| public final FileDescriptor getFD() throws IOException Returns the file descriptor associated with this stream.
| |
| initIDs | back to summary |
|---|---|
| private static native void initIDs() | |
| open | back to summary |
|---|---|
| private void open(String name, boolean append) throws FileNotFoundException Opens a file, with the specified name, for overwriting or appending.
| |
| open0 | back to summary |
|---|---|
| private native void open0(String name, boolean append) throws FileNotFoundException Opens a file, with the specified name, for overwriting or appending.
| |
| traceWrite | back to summary |
|---|---|
| private void traceWrite(int b, boolean append) throws IOException | |
| traceWriteBytes | back to summary |
|---|---|
| private void traceWriteBytes(byte[] b, int off, int len, boolean append) throws IOException | |
| write | back to summary |
|---|---|
| private native void write(int b, boolean append) throws IOException Writes the specified byte to this file output stream.
| |
| write | back to summary |
|---|---|
| public void write(int b) throws IOException Implements abstract java. Writes the specified byte to this file output stream. Implements
the
| |
| write | back to summary |
|---|---|
| public void write(byte[] b) throws IOException Overrides java. Writes
| |
| write | back to summary |
|---|---|
| public void write(byte[] b, int off, int len) throws IOException Overrides java. Writes
| |
| writeBytes | back 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.
| |