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)
将指定的字节写入该文件输出流中。
|
flush
public 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
方法拒绝写访问的文件描述符
SecurityManager.checkWrite(java.io.FileDescriptor)
public void write(int b) throws IOException
OutputStream
write
。
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
len
字节指定字节数组中的起始偏移
off
此文件输出流。
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
FileOutputStream
对象使用的连接
FileDescriptor
对象。
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.