java之通过FileChannel实现文件复制

1、FileChanel介绍

Java NIO FileChannel是连接文件的通道,从文件中读取数据和将数据写入文件。Java NIO FileChannel类是NIO用于替代使用标准Java IO API读取文件的方法。

FileInputStream的getChannel方法获取的文件通道是只读的,当然通过FileOutputStream的getChannel的方法获取的文件通道是可写的

部分API

1)、truncate截断文件功能

       /**
         * Truncates the file underlying this channel to a given size. Any bytes
         * beyond the given size are removed from the file. If there are no bytes
         * beyond the given size then the file contents are unmodified.
         * <p>
         * If the file position is currently greater than the given size, then it is
         * set to the new size.
         *
         * @param size
         *            the maximum size of the underlying file.
         * @throws IllegalArgumentException
         *             if the requested size is negative.
         * @throws ClosedChannelException
         *             if this channel is closed.
         * @throws NonWritableChannelException
         *             if the channel cannot be written to.
         * @throws IOException
         *             if another I/O error occurs.
         * @return this channel.
         */
        public abstract FileChannel truncate(long size) throws IOException;

2)、force()强制在内存中的数据刷新到硬盘中去

        /**
         * Requests that all updates to this channel are committed to the storage
         * device.
         * <p>
         * When this method returns, all modifications made to the platform file
         * underlying this channel have been committed if the file resides on a
         * local storage device. If the file is not hosted locally, for example on a
         * networked file system, then applications cannot be certain that the
         * modifications have been committed.
         * <p>
         * There are no assurances given that changes made to the file using methods
         * defined elsewhere will be committed. For example, changes made via a
         * mapped byte buffer may not be committed.
         * <p>
         * The <code>metadata</code> parameter indicates whether the update should
         * include the file's metadata such as last modification time, last access
         * time, etc. Note that passing <code>true</code> may invoke an underlying
         * write to the operating system (if the platform is maintaining metadata
         * such as last access time), even if the channel is opened read-only.
         *
         * @param metadata
         *            {@code true} if the file metadata should be flushed in
         *            addition to the file content, {@code false} otherwise.
         * @throws ClosedChannelException
         *             if this channel is already closed.
         * @throws IOException
         *             if another I/O error occurs.
         */
        public abstract void force(boolean metadata) throws IOException;

3)、transferFrom可以看出是拷贝从源的position位置的count 字节大小

        /**
         * Reads up to {@code count} bytes from {@code src} and stores them in this
         * channel's file starting at {@code position}. No bytes are transferred if
         * {@code position} is larger than the size of this channel's file. Less
         * than {@code count} bytes are transferred if there are less bytes
         * remaining in the source channel or if the source channel is non-blocking
         * and has less than {@code count} bytes immediately available in its output

更多请见:http://www.mark-to-win.com/tutorial/51789.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值