public class FileOutputStream extends OutputStream
File或一个FileDescriptor 。
文件是否可用或可能被创建取决于底层平台。
特别是某些平台允许一次只能打开一个文件来写入一个FileOutputStream (或其他文件写入对象)。
在这种情况下,如果所涉及的文件已经打开,则此类中的构造函数将失败。
FileOutputStream用于写入诸如图像数据的原始字节流。 对于写入字符流,请考虑使用FileWriter 。
File , FileDescriptor , FileInputStream , Files.newOutputStream(java.nio.file.Path, java.nio.file.OpenOption...)
| Constructor and Description |
|---|
FileOutputStream(File file)
创建文件输出流以写入由指定的
File对象表示的文件。
|
FileOutputStream(File file, boolean append)
创建文件输出流以写入由指定的
File对象表示的文件。
|
FileOutputStream(FileDescriptor fdObj)
创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。
|
FileOutputStream(String name)
创建文件输出流以指定的名称写入文件。
|
FileOutputStream(String name, boolean append)
创建文件输出流以指定的名称写入文件。
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
关闭此文件输出流并释放与此流相关联的任何系统资源。
|
protected void |
finalize()
清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的
close方法。
|
FileChannel |
getChannel()
返回与此文件输出流相关联的唯一的 FileChannel对象。
|
FileDescriptor |
getFD()
返回与此流相关联的文件描述符。
|
void |
write(byte[] b)
将
b.length个字节从指定的字节数组写入此文件输出流。
|
void |
write(byte[] b, int off, int len)
将
len字节从位于偏移量
off的指定字节数组写入此文件输出流。
|
void |
write(int b)
将指定的字节写入此文件输出流。
|
flushpublic FileOutputStream(String name) throws FileNotFoundException
FileDescriptor对象来表示此文件连接。
首先,如果有一个安全管理器,它的checkWrite方法name作为参数来调用。
如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或者由于任何其他原因无法打开,那么抛出一个FileNotFoundException 。
name - 与系统相关的文件名
FileNotFoundException - 如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或者由于任何其他原因无法打开
SecurityException - 如果安全管理器存在,并且其
checkWrite方法拒绝对该文件的写入访问。
SecurityManager.checkWrite(java.lang.String)
public FileOutputStream(String name, boolean append) throws FileNotFoundException
true ,则字节将写入文件的末尾而不是开头。
创建一个新的FileDescriptor对象来表示此文件连接。
首先,如果有一个安全管理员,它的checkWrite方法是以name作为参数来调用的。
如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或者由于任何其他原因无法打开,那么抛出一个FileNotFoundException 。
name - 与系统相关的文件名
append - 如果是
true ,那么字节将被写入文件的末尾,而不是开头
FileNotFoundException - 如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或由于任何其他原因无法打开。
SecurityException - 如果安全管理器存在,并且其
checkWrite方法拒绝对该文件的写入访问。
SecurityManager.checkWrite(java.lang.String)
public FileOutputStream(File file) throws FileNotFoundException
File对象表示的文件。
创建一个新的FileDescriptor对象来表示此文件连接。
首先,如果有一个安全管理器,它的checkWrite方法被调用与由file参数表示的路径作为其参数。
如果文件存在但是是一个目录而不是常规文件,则不存在但不能创建,或者由于任何其他原因无法打开,因此抛出FileNotFoundException 。
file - 要打开的文件进行写入。
FileNotFoundException - 如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或由于任何其他原因无法打开
SecurityException - 如果安全管理器存在,并且其
checkWrite方法拒绝对该文件的写入访问。
File.getPath() , SecurityException , SecurityManager.checkWrite(java.lang.String)
public FileOutputStream(File file, boolean append) throws FileNotFoundException
File对象表示的文件。
如果第二个参数是true ,则字节将被写入文件的末尾而不是开头。
创建一个新的FileDescriptor对象来表示此文件连接。
首先,如果有一个安全管理器,则调用其checkWrite方法,其中以file参数表示的路径作为参数。
如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或者由于任何其他原因无法打开,那么将抛出一个FileNotFoundException 。
file - 要打开的文件写入。
append - 如果
true ,则字节将被写入文件的末尾而不是开头
FileNotFoundException - 如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或由于任何其他原因无法打开
SecurityException - 如果安全管理器存在,并且其
checkWrite方法拒绝对该文件的写入访问。
File.getPath() , SecurityException , SecurityManager.checkWrite(java.lang.String)
public FileOutputStream(FileDescriptor fdObj)
首先,如果有一个安全管理器,它的checkWrite方法被调用与文件描述符fdObj参数作为其参数。
如果fdObj为null,则抛出NullPointerException 。
如果fdObj是invalid,这个构造函数不会抛出异常 。 但是,如果在结果流上调用方法来尝试流上的I / O,则会抛出一个IOException 。
fdObj - 要打开来写入的文件描述符
SecurityException - 如果安全管理器存在,并且其
checkWrite方法拒绝对
checkWrite写入访问
SecurityManager.checkWrite(java.io.FileDescriptor)
public void write(int b)
throws IOException
write方法OutputStream 。
write在
OutputStream
b - 要写入的字节。
IOException - 如果发生I / O错误。
public void write(byte[] b)
throws IOException
b.length字节从指定的字节数组写入此文件输出流。
write在
OutputStream
b - 数据。
IOException - 如果发生I / O错误。
OutputStream.write(byte[], int, int)
public void write(byte[] b,
int off,
int len)
throws IOException
off的指定字节数组写入
len字节到该文件输出流。
write在
OutputStream
b - 数据。
off - 数据中的起始偏移量。
len - 要写入的字节数。
IOException - 如果发生I / O错误。
public void close()
throws IOException
如果该流具有相关联的信道,则该信道也被关闭。
close在接口
Closeable
close在界面
AutoCloseable
close在
OutputStream
IOException - 如果发生I / O错误。
public final FileDescriptor getFD() throws IOException
FileDescriptor对象,表示与该
FileOutputStream对象正在使用的文件系统中的文件的
FileOutputStream 。
IOException - 如果发生I / O错误。
FileDescriptor
public FileChannel getChannel()
FileChannel对象。
返回通道的初始position将等于写入文件的字节数,除非该流是附加模式,在这种情况下,它将等于文件的大小。 将字节写入此流将相应地增加通道的位置。 通过显式地或通过写入来更改频道的位置将会改变这个流的文件位置。
protected void finalize()
throws IOException
close方法。
finalize在
Object
IOException - 如果发生I / O错误。
FileInputStream.close()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.