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中方式的文件拷贝-暂存
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这8种学生永远拿不到高分!早看早受益! 下面是一位资深班主任总结了8种成绩提不上去的原因,分别对应8类孩子,如果你...
- 这8种学生永远拿不到高分!早看早受益! 下面是一位资深班主任总结了8种成绩提不上去的原因,分别对应8类孩子,如果你...
- 1.上午刘老师从家庭场景方面为我们讲解如何搭配,深入浅出的从单品到组合,面对用户时如何去探寻用户的需求,以配...