FileChannel進(jìn)行文件復(fù)制
作者:張勇波
博主發(fā)表的文章,有的是自己原創(chuàng),有的是這些年本人從網(wǎng)上積累的,方便大家學(xué)習(xí)。
- /**
- * 導(dǎo)入
- * @param urlPath 附件相對路徑(xml存儲路徑)
- * @param path 項目絕對路徑
- * @param keyID 要導(dǎo)出信息的keyID
- * @param filepath 導(dǎo)入后路徑
- */
- private static void importCopy(String urlPath, String path, String keyID, String filepath) {
- // 生成目錄
- File f = new File(path + filepath);
- if (!f.exists()) {
- f.mkdirs();
- }
- String filename = urlPath.substring(urlPath.lastIndexOf("/") + 1);
- FileInputStream fi = null;
- FileOutputStream fo = null;
- FileChannel in = null;
- FileChannel out = null;
- try {
- fi = new FileInputStream(path + "/imp/" + keyID + "/" + filename); //源文件
- fo = new FileOutputStream(path + filepath + filename); //導(dǎo)入后文件
- in = fi.getChannel();// 得到對應(yīng)的文件通道
- out = fo.getChannel();// 得到對應(yīng)的文件通道
- in.transferTo(0, in.size(), out);// 連接兩個通道,并且從in通道讀取,然后寫入out通道
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- fi.close();
- in.close();
- fo.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
【本文是51CTO專欄作者張勇波的原創(chuàng)文章,轉(zhuǎn)載請通過51CTO獲取作者授權(quán)】
責(zé)任編輯:武曉燕
來源:
上下求索的Z先生博客