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. 沒(méi)有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);
}
2020-03-30-TestFileCopy4中方式的文件拷貝-暫存
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門席揽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人寸谜,你說(shuō)我怎么就攤上這事属桦。” “怎么了聂宾?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)巾陕。 經(jīng)常有香客問(wèn)我纪他,道長(zhǎng)茶袒,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任亡资,我火速辦了婚禮展懈,結(jié)果婚禮上来惧,老公的妹妹穿的比我還像新娘演顾。我一直安慰自己,他們只是感情好葛虐,可當(dāng)我...
- 文/花漫 我一把揭開白布棉钧。 她就那樣靜靜地躺著,像睡著了一般的诵。 火紅的嫁衣襯著肌膚如雪佑钾。 梳的紋絲不亂的頭發(fā)上,一...
- 那天瘪阁,我揣著相機(jī)與錄音邮偎,去河邊找鬼义黎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛廉涕,可吹牛的內(nèi)容都是我干的艇拍。 我是一名探鬼主播宠纯,決...
- 文/蒼蘭香墨 我猛地睜開眼婆瓜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了廉白?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤院溺,失蹤者是張志新(化名)和其女友劉穎磅轻,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谆膳,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年纯续,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布朴则,位于F島的核電站钓简,受9級(jí)特大地震影響汹想,放射性物質(zhì)發(fā)生泄漏撤蚊。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一槽唾、第九天 我趴在偏房一處隱蔽的房頂上張望匹中。 院中可真熱鬧,春花似錦顶捷、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至永高,卻和暖如春提针,著一層夾襖步出監(jiān)牢的瞬間命爬,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓家淤,卻偏偏與公主長(zhǎng)得像瑟由,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绿鸣,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 這8種學(xué)生永遠(yuǎn)拿不到高分!早看早受益哩簿! 下面是一位資深班主任總結(jié)了8種成績(jī)提不上去的原因,分別對(duì)應(yīng)8類孩子羡玛,如果你...
- 這8種學(xué)生永遠(yuǎn)拿不到高分!早看早受益馏予! 下面是一位資深班主任總結(jié)了8種成績(jī)提不上去的原因,分別對(duì)應(yīng)8類孩子呢岗,如果你...
- 1.上午劉老師從家庭場(chǎng)景方面為我們講解如何搭配,深入淺出的從單品到組合后豫,面對(duì)用戶時(shí)如何去探尋用戶的需求,以配...
- 2019.10.21.晴.星期一親子日記第14天 今天早上起來(lái)我媽做好飯了构眯,小寶昨晚咳嗽的很早龟,我?guī)缀醵紱](méi)敢怎么睡。...
- 日期: 2月12日 星期日 晴 今天和愛(ài)人去蘇寧買手機(jī)壹店,支持國(guó)貨芝加,鐘情華為。于是不假思索奔向華為妖混。 由此讓我聯(lián)...