Java基礎-FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream實現文件拷貝

Java實現文件拷貝,示例代碼如下:

/**
     * 
     * [拷貝文件] 
* * @author xiaoyuan
* @taskId
* @param toDir
*/ public static void copyFile(String toDir) { // FILE_LIST爲需要拷貝的文件列表 if (CollectionUtils.isEmpty(FILE_LIST)) { return; } FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; BufferedOutputStream bos = null; // 拷貝後的新文件名 String newFile = ""; try { for (File file : FILE_LIST) { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); if (toDir.endsWith("\\")) { newFile = toDir + file.getName(); } else { newFile = toDir + "\\" + file.getName(); } // 指定目錄不存在需要新建 File newF = new File(toDir); if (!newF.exists()) { newF.mkdirs(); } fos = new FileOutputStream(newFile); bos = new BufferedOutputStream(fos); int lentgh = 0; while ((lentgh = bis.read()) != -1) { bos.write(lentgh); } bis.close(); bos.close(); } } catch (IOException e) { throw new BaseAppException("Read File Error."); } }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章