日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

JAVASE_File类(实践)——目录拷贝

發(fā)布時(shí)間:2025/3/21 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVASE_File类(实践)——目录拷贝 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 實(shí)現(xiàn)代碼
    • 實(shí)現(xiàn)截圖
      • 執(zhí)行前截圖
      • 執(zhí)行后截圖

實(shí)現(xiàn)代碼

package Filecopy01;import java.io.*;/* * 拷貝源 * * */ public class CopyAll {public static void main(String[] args) {//拷貝源File srcFile=new File("G:\\植物大戰(zhàn)僵尸年度英文版");//拷貝目標(biāo)File destFile=new File("E:\\");//調(diào)用拷貝方法copyDir(srcFile,destFile);}private static void copyDir(File srcFile,File destFile){if(srcFile.isFile()){FileInputStream in=null;FileOutputStream out=null;try {//讀這個(gè)文件in=new FileInputStream(srcFile);//寫到這個(gè)文件中String destDir=(destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\\")+srcFile.getAbsolutePath().substring(3);out=new FileOutputStream(destDir);//一邊讀一邊寫byte[]bytes=new byte[1024*1024];int readCount=0;while((readCount=in.read(bytes))!=-1){out.write(bytes,0,readCount);}out.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(in!=null){try {in.close();} catch (IOException e) {e.printStackTrace();}}if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}}}//如果是一個(gè)文件的話,遞歸結(jié)束return;}File []files=srcFile.listFiles();for(File file:files){if(file.isDirectory()){String srcDir=file.getAbsolutePath();String destDir=(destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\\")+srcDir.substring(3);File newFile=new File(destDir);if(!newFile.exists()){newFile.mkdirs();}}//遞歸調(diào)用copyDir(file,destFile);}} }

實(shí)現(xiàn)截圖

執(zhí)行前截圖

執(zhí)行后截圖


總結(jié)

以上是生活随笔為你收集整理的JAVASE_File类(实践)——目录拷贝的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。