一、FileOutputStream函数
FileOutputStream函数主要实现将程序的内容通过这个接口将数据传入到文件中。下面我将从源码角度进行对FileOutputStream的内部方法进行介绍。
二、内部源码介绍
FileOutputStream函数主要继承于OutputStream函数
public
class FileOutputStream extends OutputStream
{
}
定义了文件描述字段
private final FileDescriptor fd;
定义是否是追加模式
private final boolean append;
定义文件通道
private FileChannel channel;
定义文件路径path
private final String path;
下面定义了一个关锁对象和一个转瞬即逝的关锁标志符
private final Object closeLock = new Object();
private volatile boolean closed = false;
创建文件输出流函数
public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false);
}
创建文件输入流函数,isAppend代表是否以追加模式写入
public FileOutputStream(String name, boolean append)
throws FileNotFoundException
{
this(name != null ? new File(name) : null, append);
}
文件输出流函数的实现方法
public FileOutputStream(File file) throws FileNotFoundException {
this(file, false);
}
定义文件输出流函数,是以追加方式写入的方法
public FileOutputStream(File file, boolean append)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
this.fd = new FileDescriptor();
fd.attach(this);
this.append = append;
this.path = name;
open(name, append);
}
用文件描述字段打开文件输出流函数
public FileOutputStream(FileDescriptor fdObj) {
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkWrite(fdObj);
}
this.fd = fdObj;
this.append = false;
this.path = null;
fd.attach(this);
}
下面调用JVM实现打开文件的方法定义
private native void open0(String name, boolean append)
throws FileNotFoundException;
打开文件并以追加方式写入数据
private void open(String name, boolean append)
throws FileNotFoundException {
open0(name, append);
}
调用JVM实现以追加方式写入数据
private native void write(int b, boolean append) throws IOException;
write的一个实现类,向文件中写入一个整型数据
public void write(int b) throws IOException {
write(b, append);
}
调用JVM实现向文件中写入byte数组
private native void writeBytes(byte b[], int off, int len, boolean append)
throws IOException;
向文件中写入byte数组的实现类
public void write(byte b[]) throws IOException {
writeBytes(b, 0, b.length, append);
}
向文件中写入byte数组的某一部分
public void write(byte b[], int off, int len) throws IOException {
writeBytes(b, off, len, append);
}
调用JVM实现文件关闭
private native void close0() throws IOException;
实现关闭文件输出流函数
public void close() throws IOException {
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
}
});
}
定义获取文件描述字符
public final FileDescriptor getFD() throws IOException {
if (fd != null) {
return fd;
}
throw new IOException();
}
定义实现文件管道
public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, path, false, true, append, this);
}
return channel;
}
}
清除文件连接,彻底释放文件
protected void finalize() throws IOException {
if (fd != null) {
if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
flush();
} else {
/* if fd is shared, the references in FileDescriptor
* will ensure that finalizer is only called when
* safe to do so. All references using the fd have
* become unreachable. We can call close()
*/
close();
}
}
}
定义初始化ID
private static native void initIDs();
静态代码块,获取初始化ID
static {
initIDs();
}