欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

NIO FileChannel通道+ByteBuffer完成文件复制

程序员文章站 2022-10-03 16:53:07
NIO FileChannel通道File接口:源码:public interface Channel extends Closeable { /** * Tells whether or not this channel is open. * * @return true if, and only if, this channel is open */ public boolean isOpen(...

NIO FileChannel通道

NIO FileChannel通道+ByteBuffer完成文件复制

File接口:
NIO FileChannel通道+ByteBuffer完成文件复制

源码:

public interface Channel extends Closeable {

    /**
     * Tells whether or not this channel is open.
     *
     * @return <tt>true</tt> if, and only if, this channel is open
     */
    public boolean isOpen();

    /**
     * Closes this channel.
     *
     * <p> After a channel is closed, any further attempt to invoke I/O
     * operations upon it will cause a {@link ClosedChannelException} to be
     * thrown.
     *
     * <p> If this channel is already closed then invoking this method has no
     * effect.
     *
     * <p> This method may be invoked at any time.  If some other thread has
     * already invoked it, however, then another invocation will block until
     * the first invocation is complete, after which it will return without
     * effect. </p>
     *
     * @throws  IOException  If an I/O error occurs
     */
    public void close() throws IOException;

可以从顶层的Channel接口看到,该接口只提供了两个方法,通道是否打开、关闭通道,所有的附加功能石油它的实现类和子类完成。

先看看之前的普通IO是如何读取数据的

在电脑D 盘某文件下新建一个文件
NIO FileChannel通道+ByteBuffer完成文件复制

看看之前普通io是怎么复制文件的

public static void main(String[] args) throws Exception{
        File file = new File("D:\\hufan","love.txt");
        File outFile = new File("D:\\hufan","fan.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
                outFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //普通IO
        //输入流(由于文件中存的是中文,所以用了字节流,避免乱码)
        FileInputStream fileInputStream = new FileInputStream(file);
        //输出流
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        String s = null;
        byte[] bytes = new byte[1024];
        while (fileInputStream.read(bytes,0,1024) != -1){
             s = new String(bytes,0,1024);
             fileOutputStream.write(bytes,0,1024);
        }
        //关闭流
        fileInputStream.close();
        fileOutputStream.close();
        System.out.println(s);

    }

看看输出效果:
NIO FileChannel通道+ByteBuffer完成文件复制
可见输出流是没有问题的,再看看文件是否复制成功

NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制
用之前普通IO完成了复制功能,接下来用NIO完成,直接上代码:

public static void main(String[] args) throws Exception{
        File file = new File("D:\\hufan","love.txt");
        File file1 = new File("D:\\hufan","hu.txt");
        file.createNewFile();
        file1.createNewFile();
        FileChannel fileChannel = FileChannel.open(file.toPath());
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //创建通道
        FileChannel write = FileChannel.open(file1.toPath(), StandardOpenOption.WRITE);
        while (fileChannel.read(byteBuffer) != -1){
            //由读改为写
            byteBuffer.flip();
            write.write(byteBuffer);
            //清空缓存区
            byteBuffer.clear();
        }
        //关闭通道
        fileChannel.close();
        write.close();

    }

NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制
NIO FileChannel通道+ByteBuffer完成文件复制

NIO FileChannel通道+ByteBuffer完成文件复制

本文地址:https://blog.csdn.net/weixin_43481793/article/details/107494671

相关标签: 学习