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

NIO FileChannel通道

NIO FileChannel通道+ByteBuffer完成文件复制_第1张图片

File接口:
NIO FileChannel通道+ByteBuffer完成文件复制_第2张图片

源码:

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();

    /**
     * Closes this channel.
     *
     * 

After a channel is closed, any further attempt to invoke I/O * operations upon it will cause a {@link ClosedChannelException} to be * thrown. * *

If this channel is already closed then invoking this method has no * effect. * *

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.

* * @throws IOException If an I/O error occurs */ public void close() throws IOException;

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

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

在电脑D 盘某文件下新建一个文件
NIO FileChannel通道+ByteBuffer完成文件复制_第3张图片

看看之前普通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完成文件复制_第4张图片
可见输出流是没有问题的,再看看文件是否复制成功

NIO FileChannel通道+ByteBuffer完成文件复制_第5张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第6张图片
用之前普通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完成文件复制_第7张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第8张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第9张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第10张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第11张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第12张图片
NIO FileChannel通道+ByteBuffer完成文件复制_第13张图片

NIO FileChannel通道+ByteBuffer完成文件复制_第14张图片

你可能感兴趣的:(学习)