2020-03-30-TestFileCopy4中方式的文件拷贝-暂存

package com.niewj.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author weijun.nie
 * @Date 2020/3/30 8:18
 * @Version 1.0
 */
public class TestFileCopy {

    public static void close(Closeable closeable){
        if(closeable != null){
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        // 1. 没有buffer的 java.iostream
        FileCopyRunner nobufferStreamCopy= new FileCopyRunner() {
            @Override
            public void copyFile(File source, File target) {
                FileInputStream fIn = null;
                FileOutputStream fOut = null;
                try {
                    fIn = new FileInputStream(source);
                    fOut = new FileOutputStream(target);
                    int n ;
                    while((n = fIn.read()) != -1){
                        fOut.write(n);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    close(fIn);
                    close(fOut);
                }

            }
        };
        FileCopyRunner bufferStreamCopy = new FileCopyRunner() {
            @Override
            public void copyFile(File source, File target) {
                BufferedInputStream bin = null;
                BufferedOutputStream bout = null;
                try {
                     bin = new BufferedInputStream(new FileInputStream(source));
                     bout = new BufferedOutputStream(new FileOutputStream(target));
                    byte[] buff = new byte[1024];
                    int len = -1;
                    while((len = bin.read(buff)) != -1){
                        bout.write(buff, 0, len);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    close(bin);
                    close(bout);
                }
            }
        };
        FileCopyRunner nioCopy = new FileCopyRunner() {
            @Override
            public void copyFile(File source, File target) {
                FileChannel channelIn = null;
                FileChannel channelout = null;
                try {
                    channelIn = new FileInputStream(source).getChannel();
                    channelout = new FileOutputStream(target).getChannel();

                    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    int len = -1;
                    while((len = channelIn.read(byteBuffer)) != -1){
                        // flip后进入buff读模式, position和limit浮动
                        byteBuffer.flip(); 
                        while(byteBuffer.hasRemaining()){
                            channelout.write(byteBuffer);
                        }
                        // buff读完后clear, 清理buff并进入写模式, 准备下一次写(将channel中内容写入到buff)
                        byteBuffer.clear();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    close(channelIn);
                    close(channelout);
                }

            }
        };
        FileCopyRunner channelTransferCopy = new FileCopyRunner() {
            @Override
            public void copyFile(File source, File target) {
                FileChannel channelIn = null;
                FileChannel channelout = null;
                try {
                    channelIn = new FileInputStream(source).getChannel();
                    channelout = new FileOutputStream(target).getChannel();
                    
                    long len = source.length();
                    channelIn.transferTo(0, len, channelout);
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    close(channelIn);
                    close(channelout);
                }
            }
        };
    }
}
interface FileCopyRunner{
    void copyFile(File source, File target);
}

你可能感兴趣的:(2020-03-30-TestFileCopy4中方式的文件拷贝-暂存)