從文件夾中復制指定的文件到另一個文件夾


1、以下方法均在A類中

/**
*localPath:源文件夾的路徑
*filterIrpFileFolderPath:存放復制后的文件的路徑
*fileType:過濾要復制的文件的條件
*/
private
void filterIrpFiles(String localPath, String filterIrpFileFolderPath, String fileType) { if (StringUtil.isEmpty(localPath) || StringUtil.isEmpty(filterIrpFileFolderPath)) { return; } File folder = new File(localPath); if (!folder.exists()) { return; } File[] files = folder.listFiles(); if (null == files) { return; } // 創建文件夾 File dir = new File(filterIrpFileFolderPath); if (!dir.exists()) { dir.mkdirs(); } for (File file : files) { if (!file.isFile()) { continue; } if (!file.getName().trim().contains(fileType)) { continue; } copyFile(filterIrpFileFolderPath, file); } }
private void copyFile(String filterIrpFileFolderPath, File file) { InputStream is = null; OutputStream os = null; try { File osFile = new File(filterIrpFileFolderPath + File.separator + file.getName().trim()); if (!osFile.exists()) { osFile.createNewFile(); } is = new FileInputStream(file); os = new FileOutputStream(osFile); byte[] flush = new byte[1024]; int len = -1; while((len = is.read(flush)) != -1) { os.write(flush, 0, len); }        // 刷新緩存 os.flush(); } catch (IOException e) { logger.error(e); } finally { if (os != null) { try { os.close(); } catch (IOException e) { log.error(e); } } if (is != null) { try { is.close(); } catch (IOException e) { log.error(e); } } } }

TEST:

public static void main(String[] args) { A a= new A(); a.filterIrpFiles("D:\\AX_EDI\\HaviToGodiva", "D:\\EC_EDI\\HaviToGodiva", "DC"); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM