国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java使用遞歸復(fù)制文件夾及文件夾

瀏覽:3日期:2022-09-01 16:23:14

遞歸調(diào)用copyDir方法實(shí)現(xiàn),查詢?cè)次募夸浭褂米止?jié)輸入流寫入字節(jié)數(shù)組,如果目標(biāo)文件目錄沒有就創(chuàng)建目錄,如果迭代出是文件夾使用字節(jié)輸出流對(duì)拷文件,直至源文件目錄沒有內(nèi)容。

/** * 復(fù)制文件夾 * @param srcDir 源文件目錄 * @param destDir 目標(biāo)文件目錄 */ public static void copyDir(String srcDir, String destDir) { if (srcRoot == null) srcRoot = srcDir; //源文件夾 File srcFile = new File(srcDir); //目標(biāo)文件夾 File destFile = new File(destDir); //判斷srcFile有效性 if (srcFile == null || !srcFile.exists()) return; //創(chuàng)建目標(biāo)文件夾 if (!destFile.exists()) destFile.mkdirs(); //判斷是否是文件 if (srcFile.isFile()) { //源文件的絕對(duì)路徑 String absPath = srcFile.getAbsolutePath(); //取出上級(jí)目錄 String parentDir = new File(srcRoot).getParent(); //拷貝文件 copyFile(srcFile.getAbsolutePath(), destDir); } else { //如果是目錄 File[] children = srcFile.listFiles(); if (children != null) {for (File f : children) { copyDir(f.getAbsolutePath(), destDir);} } } }

/** * 復(fù)制文件夾 * * @param path 路徑 * @param destDir 目錄 */ public static void copyFile(String path, String destDir) { FileInputStream fis = null; FileOutputStream fos = null; try { /*準(zhǔn)備目錄取出相對(duì)的路徑創(chuàng)建目標(biāo)文件所在的文件目錄 */ String tmp = path.substring(srcRoot.length()); String folder = new File(destDir, tmp).getParentFile().getAbsolutePath(); File destFolder = new File(folder); destFolder.mkdirs(); System.out.println(folder); //創(chuàng)建文件輸入流 fis = new FileInputStream(path); //定義新路徑 //創(chuàng)建文件 輸出流 fos = new FileOutputStream(new File(destFolder,new File(path).getName())); //創(chuàng)建字節(jié)數(shù)組進(jìn)行流對(duì)拷 byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) {fos.write(buf, 0, len); } } catch (IOException ex) { ex.printStackTrace(); } finally { try {fis.close();fos.close(); } catch (IOException e) {e.printStackTrace(); } } }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 栾川县| 克拉玛依市| 合江县| 木里| 宁波市| 灌阳县| 大悟县| 天祝| 桃江县| 商城县| 专栏| 犍为县| 吴旗县| 富顺县| 南宁市| 商水县| 石楼县| 九台市| 阿拉尔市| 镇安县| 樟树市| 奉化市| 青神县| 德庆县| 呼图壁县| 昌图县| 宝应县| 浦江县| 永顺县| 乌鲁木齐市| 丹东市| 玉环县| 尉氏县| 上杭县| 宣恩县| 保德县| 静宁县| 三门峡市| 岳普湖县| 准格尔旗| 平罗县|