Class FileOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FileOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class FileOutputStream extends OutputStream
A file output stream is an output stream for writing data to aFile. 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.FileOutputStreamis meant for writing streams of raw bytes such as image data. For writing streams of characters, consider usingFileWriter.- Since:
- JDK1.0
- See Also:
File,FileInputStream
-
-
Constructor Summary
Constructors Constructor Description FileOutputStream(File file)Creates a file output stream to write to the file represented by the specifiedFileobject.FileOutputStream(File file, boolean append)Creates a file output stream to write to the file represented by the specifiedFileobject.FileOutputStream(String name)Creates a file output stream to write to the file with the specified name.FileOutputStream(String name, boolean append)Creates a file output stream to write to the file with the specified name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this file output stream and releases any system resources associated with this stream.voidwrite(byte[] b)Writesb.lengthbytes from the specified byte array to this file output stream.voidwrite(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this file output stream.voidwrite(int b)Writes the specified byte to this file output stream.-
Methods inherited from class java.io.OutputStream
flush
-
-
-
-
Constructor Detail
-
FileOutputStream
public FileOutputStream(String name) throws FileNotFoundException
Creates a file output stream to write to the file with the specified name.First, if there is a security manager, its
checkWritemethod is called withnameas 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
FileNotFoundExceptionis thrown.- Parameters:
name- the system-dependent filename- Throws:
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 reasonSecurityException- if a security manager exists and itscheckWritemethod denies write access to the file.
-
FileOutputStream
public FileOutputStream(String name, boolean append) throws FileNotFoundException
Creates a file output stream to write to the file with the specified name. If the second argument istrue, then bytes will be written to the end of the file rather than the beginning.First, if there is a security manager, its
checkWritemethod is called withnameas 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
FileNotFoundExceptionis thrown.- Parameters:
name- the system-dependent file nameappend- iftrue, then bytes will be written to the end of the file rather than the beginning- Throws:
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- if a security manager exists and itscheckWritemethod denies write access to the file.- Since:
- JDK1.1
-
FileOutputStream
public FileOutputStream(File file) throws FileNotFoundException
Creates a file output stream to write to the file represented by the specifiedFileobject.First, if there is a security manager, its
checkWritemethod is called with the path represented by thefileargument 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
FileNotFoundExceptionis thrown.- Parameters:
file- the file to be opened for writing.- Throws:
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 reasonSecurityException- if a security manager exists and itscheckWritemethod denies write access to the file.- See Also:
File.getPath(),SecurityException
-
FileOutputStream
public FileOutputStream(File file, boolean append) throws FileNotFoundException
Creates a file output stream to write to the file represented by the specifiedFileobject. If the second argument istrue, then bytes will be written to the end of the file rather than the beginning.First, if there is a security manager, its
checkWritemethod is called with the path represented by thefileargument 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
FileNotFoundExceptionis thrown.- Parameters:
file- the file to be opened for writing.append- iftrue, then bytes will be written to the end of the file rather than the beginning- Throws:
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 reasonSecurityException- if a security manager exists and itscheckWritemethod denies write access to the file.- Since:
- 1.4
- See Also:
File.getPath(),SecurityException
-
-
Method Detail
-
write
public void write(int b) throws IOExceptionWrites the specified byte to this file output stream. Implements thewritemethod ofOutputStream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to be written.- Throws:
IOException- if an I/O error occurs.
-
write
public void write(byte[] b) throws IOExceptionWritesb.lengthbytes from the specified byte array to this file output stream.- Overrides:
writein classOutputStream- Parameters:
b- the data.- Throws:
IOException- if an I/O error occurs.- See Also:
OutputStream.write(byte[], int, int)
-
write
public void write(byte[] b, int off, int len) throws IOExceptionWriteslenbytes from the specified byte array starting at offsetoffto this file output stream.- Overrides:
writein classOutputStream- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
IOException- if an I/O error occurs.
-
close
public void close() throws IOExceptionCloses 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an I/O error occurs.
-
-