JDK1.8源码学习--io包(FileOutputStream)

前言
 

月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)

央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)

泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)

月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容

希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
 

一.FileOutputStream的作用:

                直接看源码注释(我的翻译可能不太准,如果道友们有更棒的理解,可以留言或者私信)

/**
 * A file output stream is an output stream for writing data to a
 * <code>File</code> or to a <code>FileDescriptor</code>. 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 <tt>FileOutputStream</tt> (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.
 * 1.文件输出流是用于将数据写入File或FileDescriptor的输出流。文件是否可用或可以创建取决于底层平台。
 * 特别是某些平台,一次只允许一个FileOutputStream(或其他文件写入对象)打开一个文件进行写入。
 * 在这种情况下,如果所涉及的文件已经打开,则此类中的构造函数将失败
 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
 * such as image data. For writing streams of characters, consider using
 * <code>FileWriter</code>.
 * 2.FileOutputStream用于写入原始字节流,例如图像数据。要写入字符流,请考虑使用FileWriter
 * @author  Arthur van Hoff
 * @see     java.io.File
 * @see     java.io.FileDescriptor
 * @see     java.io.FileInputStream
 * @see     java.nio.file.Files#newOutputStream
 * @since   JDK1.0
 */

二.类图:

 三.成员变量: 

    /**
     * The system dependent file descriptor.
     * 系统相关文件描述符
     */
    private final FileDescriptor fd;

    /**
     * True if the file is opened for append.
     * 如果打开文件进行追加,则为真
     */
    private final boolean append;

    /**
     * The associated channel, initialized lazily.
     * 关联的通道,延迟初始化
     */
    private FileChannel channel;

    /**
     * The path of the referenced file
     * (null if the stream is created with a file descriptor)
     * 引用文件的路径(如果流是用文件描述符创建的,则为 null)
     */
    private final String path;

    private final Object closeLock = new Object();
    private volatile boolean closed = false;

四.构造方法:

        /
     * 1.创建文件输出流以写入具有指定名称的文件。创建一个新的FileDescriptor对象来表示此文件连接
     * 2.首先,如果有一个安全管理器,它的checkWrite方法被调用,
     * name作为它的参数
     * 3.如果文件存在但是是一个目录而不是常规文件,不存在但无法创建,
     * 或者由于任何其他原因无法打开,则抛出FileNotFoundException
     */
    public FileOutputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null, false);
    }

    /**
     * 1.创建文件输出流以写入具有指定名称的文件。如果第二个参数是true,
     * 则字节将写入文件的末尾而不是开头。创建一个新的FileDescriptor对象来表示此文件连接
     * 2.首先,如果有一个安全管理器,它的checkWrite方法被调用,name作为它的参数。
     * 3.如果文件存在但是是一个目录而不是常规文件,不存在但无法创建,
     * 或者由于任何其他原因无法打开,则抛出FileNotFoundException
     */
    public FileOutputStream(String name, boolean append)
        throws FileNotFoundException
    {
        this(name != null ? new File(name) : null, append);
    }

    /**
     * 1.创建文件输出流以写入由指定的File对象表示的文件。
     * 创建一个新的FileDescriptor对象来表示此文件连接
     * 2.首先,如果有一个安全管理器,它的checkWrite方法被调用,以file参数表示的路径作为它的参数
     * 3.如果文件存在但是是一个目录而不是常规文件,不存在但无法创建,
     * 或者由于任何其他原因无法打开,则抛出FileNotFoundException
     */
    public FileOutputStream(File file) throws FileNotFoundException {
        this(file, false);
    }

    /**
     * 1.创建文件输出流以写入由指定的File对象表示的文件。如果第二个参数是true,
     * 则字节将写入文件的末尾而不是开头。创建一个新的FileDescriptor对象来表示此文件连接。
     * 2.首先,如果有一个安全管理器,它的checkWrite方法被调用,以file参数表示的路径作为它的参数
     * 3.如果文件存在但是是一个目录而不是常规文件,不存在但无法创建,
     * 或者由于任何其他原因无法打开,则抛出FileNotFoundException
     */
    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);
    }

    /**
     * 1.创建文件输出流以写入指定的文件描述符,该文件描述符表示与文件系统中实际文件的现有连接
     * 2.首先,如果有一个安全管理器,它的checkWrite方法被调用,
     * 文件描述符fdObj参数作为它的参数

     * 3.如果fdObj为 null,则抛出NullPointerException。
     * 4.如果fdObj是 java.io.FileDescriptor.valid(),这个构造函数不会抛出异常。
     * 但是,如果在结果流上调用这些方法以尝试对流进行 IO,则会引发IOException
     */
    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);
    }

五.内部方法:

                write

    /**
     * 将指定字节写入此文件输出流。实现OutputStream的write方法
     */
    public void write(int b) throws IOException {
        write(b, append);
    }

    /**
     * 将指定字节数组中的b.length字节写入此文件输出流
     */
    public void write(byte b[]) throws IOException {
        writeBytes(b, 0, b.length, append);
    }

    /**
     * 将len字节从从偏移量off开始的指定字节数组写入此文件输出流
     */
    public void write(byte b[], int off, int len) throws IOException {
        writeBytes(b, off, len, append);
    }

                close

    /**
     * 1.关闭此文件输出流并释放与此流关联的所有系统资源。此文件输出流可能不再用于写入字节
 
     * 2.如果此流具有关联的通道,则该通道也将关闭
     */
    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();
           }
        });
    }

                getFD


    /**
     * 返回与此流关联的文件描述符
     */
     public final FileDescriptor getFD()  throws IOException {
        if (fd != null) {
            return fd;
        }
        throw new IOException();
     }

                getChannel

    /**
     * 1.返回与此文件输出流关联的唯一 java.nio.channels.FileChannel对象。
     * 2.返回通道的初始 java.nio.channels.FileChannel.position()
     * 将等于到目前为止写入文件的字节数,除非此流处于追加模式,在这种情况下它将等于文件的大小。
     * 将字节写入此流将相应地增加通道的位置。显式或通过写入更改通道的位置将更改此流的文件位置
     */
    public FileChannel getChannel() {
        synchronized (this) {
            if (channel == null) {
                channel = FileChannelImpl.open(fd, path, false, true, append, this);
            }
            return channel;
        }
    }

  

六.总结:         

                基础的文件操作流,好像也没什么说的...

              

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值