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后進(jìn)入buff讀模式, position和limit浮動(dòng)
                        byteBuffer.flip(); 
                        while(byteBuffer.hasRemaining()){
                            channelout.write(byteBuffer);
                        }
                        // buff讀完后clear, 清理buff并進(jìn)入寫模式, 準(zhǔn)備下一次寫(將channel中內(nèi)容寫入到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);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容