文件的复制
/**?
?*?下載本地文件?
? *?@author?柳松?
?*?@date?2015-12-30?下午3:27:02?
? *?@throws?Exception?
? */??
private?static?void?downLoadLocalFile()?throws?Exception{??
?? ??File?file?=?new?File("D:/file/1.txt");??
?? ??FileInputStream?in?=?new?FileInputStream(file);??
//定義輸出的路徑??
?? ??File?saveDir?=?new?File("D:/file/fileCopy");??
if?(!saveDir.exists())?{??
???????? saveDir.mkdirs();//創(chuàng)建多重目錄??
?? ??}??
? ???FileOutputStream?os?=?new?FileOutputStream(saveDir+"/"+file.getName());??
//創(chuàng)建緩沖區(qū)??
byte?buffer[]?=?new?byte[1024];??
int?len?=?0;??
//?循環(huán)將輸入流中的內(nèi)容讀取到緩沖區(qū)當(dāng)中??
while?((len?=?in.read(buffer))?>?0)?{??
???? ?? os.write(buffer,?0,?len);??
??? ?}??
?? ??in.close();??
?? ??os.close();??
}?
?
/**?
????? *?下載網(wǎng)絡(luò)文件?
????? *?@author?柳松?
????? *?@date?2015-12-30?下午3:27:19?
????? *?@throws?Exception?
????? */??
private?static?void?downLoadRemoteFile()?throws?Exception{??
??????? ?URL?url?=?new?URL("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");??
???????? InputStream?in?=?url.openStream();??
//定義輸出的路徑??
??????? ?File?saveDir?=?new?File("D:/file/fileCopy");??
if?(!saveDir.exists())?{??
??????? ?????saveDir.mkdirs();//創(chuàng)建多重目錄??
??????? ?}??
??????? ?FileOutputStream?os?=?new?FileOutputStream(saveDir+"/"+"downLoad.jpg");??
//創(chuàng)建緩沖區(qū)??
byte?buffer[]?=?new?byte[1024];??
int?len?=?0;??
//?循環(huán)將輸入流中的內(nèi)容讀取到緩沖區(qū)當(dāng)中??
while?((len?=?in.read(buffer))?>?0)?{??
???????? ????os.write(buffer,?0,?len);??
??????? ?}??
??????? ?in.close();??
?????? ??os.close();??
??? ?}?
轉(zhuǎn)載于:https://www.cnblogs.com/zhaoleigege/p/7595906.html
總結(jié)
- 上一篇: ps 切图 笔记
- 下一篇: 阿里巴巴拿下奥运顶级赞助商