java.io
Class FileOutputStream

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream 
      extended by java.io.FileOutputStream
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Flushable sample code for java.io.Flushable definition code for java.io.Flushable

public class FileOutputStream
extends OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream

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.

Since:
JDK1.0
See Also:
File sample code for java.io.File definition code for java.io.File , FileDescriptor sample code for java.io.FileDescriptor definition code for java.io.FileDescriptor , FileInputStream sample code for java.io.FileInputStream definition code for java.io.FileInputStream

Constructor Summary
FileOutputStream sample code for java.io.FileOutputStream.FileOutputStream(java.io.File) definition code for java.io.FileOutputStream.FileOutputStream(java.io.File) (File sample code for java.io.File definition code for java.io.File  file)
          Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream sample code for java.io.FileOutputStream.FileOutputStream(java.io.File, boolean) definition code for java.io.FileOutputStream.FileOutputStream(java.io.File, boolean) (File sample code for java.io.File definition code for java.io.File  file, boolean append)
          Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream sample code for java.io.FileOutputStream.FileOutputStream(java.io.FileDescriptor) definition code for java.io.FileOutputStream.FileOutputStream(java.io.FileDescriptor) (FileDescriptor sample code for java.io.FileDescriptor definition code for java.io.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.
FileOutputStream sample code for java.io.FileOutputStream.FileOutputStream(java.lang.String) definition code for java.io.FileOutputStream.FileOutputStream(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Creates an output file stream to write to the file with the specified name.
FileOutputStream sample code for java.io.FileOutputStream.FileOutputStream(java.lang.String, boolean) definition code for java.io.FileOutputStream.FileOutputStream(java.lang.String, boolean) (String sample code for java.lang.String definition code for java.lang.String  name, boolean append)
          Creates an output file stream to write to the file with the specified name.
 
Method Summary
 void close sample code for java.io.FileOutputStream.close() definition code for java.io.FileOutputStream.close() ()
          Closes this file output stream and releases any system resources associated with this stream.
protected  void finalize sample code for java.io.FileOutputStream.finalize() definition code for java.io.FileOutputStream.finalize() ()
          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.
 FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel getChannel sample code for java.io.FileOutputStream.getChannel() definition code for java.io.FileOutputStream.getChannel() ()
          Returns the unique FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel object associated with this file output stream.
 FileDescriptor sample code for java.io.FileDescriptor definition code for java.io.FileDescriptor getFD sample code for java.io.FileOutputStream.getFD() definition code for java.io.FileOutputStream.getFD() ()
          Returns the file descriptor associated with this stream.
 void write sample code for java.io.FileOutputStream.write(byte[]) definition code for java.io.FileOutputStream.write(byte[]) (byte[] b)
          Writes b.length bytes from the specified byte array to this file output stream.
 void write sample code for java.io.FileOutputStream.write(byte[], int, int) definition code for java.io.FileOutputStream.write(byte[], int, int) (byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this file output stream.
 void write sample code for java.io.FileOutputStream.write(int) definition code for java.io.FileOutputStream.write(int) (int b)
          Writes the specified byte to this file output stream.
 
Methods inherited from class java.io.OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
flush sample code for java.io.OutputStream.flush() definition code for java.io.OutputStream.flush()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

FileOutputStream sample code for java.io.FileOutputStream(java.lang.String) definition code for java.io.FileOutputStream(java.lang.String)

public FileOutputStream(String sample code for java.lang.String definition code for java.lang.String  name)
                 throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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.

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 - the system-dependent filename
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
See Also:
SecurityManager.checkWrite(java.lang.String) sample code for java.lang.SecurityManager.checkWrite(java.lang.String) definition code for java.lang.SecurityManager.checkWrite(java.lang.String)

FileOutputStream sample code for java.io.FileOutputStream(java.lang.String, boolean) definition code for java.io.FileOutputStream(java.lang.String, boolean)

public FileOutputStream(String sample code for java.lang.String definition code for java.lang.String  name,
                        boolean append)
                 throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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.

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 - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
Since:
JDK1.1
See Also:
SecurityManager.checkWrite(java.lang.String) sample code for java.lang.SecurityManager.checkWrite(java.lang.String) definition code for java.lang.SecurityManager.checkWrite(java.lang.String)

FileOutputStream sample code for java.io.FileOutputStream(java.io.File) definition code for java.io.FileOutputStream(java.io.File)

public FileOutputStream(File sample code for java.io.File definition code for java.io.File  file)
                 throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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.

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 - the file to be opened for writing.
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
See Also:
File.getPath() sample code for java.io.File.getPath() definition code for java.io.File.getPath() , SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException , SecurityManager.checkWrite(java.lang.String) sample code for java.lang.SecurityManager.checkWrite(java.lang.String) definition code for java.lang.SecurityManager.checkWrite(java.lang.String)

FileOutputStream sample code for java.io.FileOutputStream(java.io.File, boolean) definition code for java.io.FileOutputStream(java.io.File, boolean)

public FileOutputStream(File sample code for java.io.File definition code for java.io.File  file,
                        boolean append)
                 throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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.

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 - the file to be opened for writing.
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.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
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
Since:
1.4
See Also:
File.getPath() sample code for java.io.File.getPath() definition code for java.io.File.getPath() , SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException , SecurityManager.checkWrite(java.lang.String) sample code for java.lang.SecurityManager.checkWrite(java.lang.String) definition code for java.lang.SecurityManager.checkWrite(java.lang.String)

FileOutputStream sample code for java.io.FileOutputStream(java.io.FileDescriptor) definition code for java.io.FileOutputStream(java.io.FileDescriptor)

public FileOutputStream(FileDescriptor sample code for java.io.FileDescriptor definition code for java.io.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.

Parameters:
fdObj - the file descriptor to be opened for writing
Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkWrite method denies write access to the file descriptor
See Also:
SecurityManager.checkWrite(java.io.FileDescriptor) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor)
Method Detail

write sample code for java.io.FileOutputStream.write(int) definition code for java.io.FileOutputStream.write(int)

public void write(int b)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes the specified byte to this file output stream. Implements the write method of OutputStream.

Specified by:
write sample code for java.io.OutputStream.write(int) definition code for java.io.OutputStream.write(int) in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Parameters:
b - the byte to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.

write sample code for java.io.FileOutputStream.write(byte[]) definition code for java.io.FileOutputStream.write(byte[])

public void write(byte[] b)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes b.length bytes from the specified byte array to this file output stream.

Overrides:
write sample code for java.io.OutputStream.write(byte[]) definition code for java.io.OutputStream.write(byte[]) in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Parameters:
b - the data.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
OutputStream.write(byte[], int, int) sample code for java.io.OutputStream.write(byte[], int, int) definition code for java.io.OutputStream.write(byte[], int, int)

write sample code for java.io.FileOutputStream.write(byte[], int, int) definition code for java.io.FileOutputStream.write(byte[], int, int)

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes len bytes from the specified byte array starting at offset off to this file output stream.

Overrides:
write sample code for java.io.OutputStream.write(byte[], int, int) definition code for java.io.OutputStream.write(byte[], int, int) in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.

close sample code for java.io.FileOutputStream.close() definition code for java.io.FileOutputStream.close()

public void close()
           throws IOException sample code for java.io.IOException definition code for java.io.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.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Overrides:
close sample code for java.io.OutputStream.close() definition code for java.io.OutputStream.close() in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.

getFD sample code for java.io.FileOutputStream.getFD() definition code for java.io.FileOutputStream.getFD()

public final FileDescriptor sample code for java.io.FileDescriptor definition code for java.io.FileDescriptor  getFD()
                           throws IOException sample code for java.io.IOException definition code for java.io.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.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FileDescriptor sample code for java.io.FileDescriptor definition code for java.io.FileDescriptor

getChannel sample code for java.io.FileOutputStream.getChannel() definition code for java.io.FileOutputStream.getChannel()

public FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel  getChannel()
Returns the unique FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel object associated with this file output stream.

The initial position sample code for java.nio.channels.FileChannel.position() definition code for java.nio.channels.FileChannel.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:
the file channel associated with this file output stream
Since:
1.4

finalize sample code for java.io.FileOutputStream.finalize() definition code for java.io.FileOutputStream.finalize()

protected void finalize()
                 throws IOException sample code for java.io.IOException definition code for java.io.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.

Overrides:
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() in class Object sample code for java.lang.Object definition code for java.lang.Object
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FileInputStream.close() sample code for java.io.FileInputStream.close() definition code for java.io.FileInputStream.close()