Java的IO流實(shí)現(xiàn)文件和文件夾的復(fù)制
本文實(shí)例為大家分享了Java的IO流實(shí)現(xiàn)文件和文件夾復(fù)制的具體代碼,供大家參考,具體內(nèi)容如下
1、使用文件流對(duì)單個(gè)文件進(jìn)行復(fù)制
將某個(gè)文件復(fù)制到指定的路徑下:
//復(fù)制文件 public static void copy(File f1, String path) throws IOException { ?? ??? ?System.out.println("***copy***" + f1.getName()); ?? ??? ?FileInputStream in = new FileInputStream(f1); ?? ??? ?FileOutputStream out = new FileOutputStream(path + f1.getName()); ?? ??? ?byte[] bytes = new byte[512]; ?? ??? ?while ((in.read(bytes)) != -1) { ?? ??? ??? ?out.write(bytes, 0, bytes.length); ?? ??? ?} ?? ??? ?out.close(); ?? ??? ?in.close(); ?? ?}
2、使用文件流將文件及文件夾下的文件進(jìn)行復(fù)制
將a文件夾下的文件拷貝到b文件夾里。a和b文件夾可能存在相同文件,若b文件夾下已經(jīng)存在a文件夾中的文件則不拷貝,若不存在則拷貝。
(1)遞歸復(fù)制文件夾下的文件
public static void copyAll(File A, File B) throws IOException { ?? ??? ?boolean flag = true; ?? ??? ?if (!B.exists()) { ?? ??? ??? ?B.mkdir(); ?? ??? ?} ?? ??? ?String[] a=A.list(); ?? ??? ?//遍歷A中所有文件及文件夾 ?? ??? ?for (int i = 0; i < a.length; i++) { ?? ??? ??? ?System.out.println("a:"+a[i]); ?? ??? ??? ?//若A中含有文件夾 ?? ??? ??? ?if (new File(A.getPath()+"/"+a[i]).isDirectory()) { ?? ??? ??? ??? ?File newFolder=null; ?? ??? ??? ??? ?if (! ( new File(B.getPath()+"/"+a[i]).exists() )) { ?? ??? ??? ??? ??? ?newFolder = new File(B, a[i]); ?? ??? ??? ??? ??? ?newFolder.mkdir(); //創(chuàng)建子文件夾 ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//遞歸,判斷 ?? ??? ??? ??? ?copyAll(new File(A.getPath()+"/"+a[i]), newFolder); ?? ??? ??? ?} ?? ??? ??? ?//若A中含有文件,直接復(fù)制文件 ?? ??? ??? ?else { ?? ??? ??? ??? ?copyOtherFile(new File(A.getPath()+"/"+a[i]), B); ?? ??? ??? ?}?? ??? ??? ? ?? ??? ?} ?? ??? ? ?? ?}
(2)若含有同名文件,則不進(jìn)行復(fù)制
public static void copyOtherFile(File srcfile, File destfile) throws IOException { ?? ??? ?File[] destlist = destfile.listFiles(); ?? ??? ?boolean flag = true; ?? ??? ??? ?flag = true; ?? ??? ??? ?for (File f2 : destlist) { ?? ??? ??? ??? ?System.out.println("f:" + srcfile.getName()); ?? ??? ??? ??? ?System.out.println("f2:" + f2.getName()); ?? ??? ??? ??? ?//含有同名文件,不進(jìn)行復(fù)制 ?? ??? ??? ??? ?if (srcfile.getName().equals(f2.getName())) { ?? ??? ??? ??? ??? ?System.out.println("相同"); ?? ??? ??? ??? ??? ?flag = false; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?System.out.println("不相同"); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?if (flag) { ?? ??? ??? ??? ?copy(srcfile, destfile.getPath() + "/"); ?? ??? ??? ?} ?? ??? ? ?? ?}
(3)主函數(shù)調(diào)用
public static void main(String[] args) throws IOException { ?? ??? ?File file = new File("F:/a"); ?? ??? ?File file2 = new File("F:/b"); ?? ??? ?copyAll(file, file2); ?? ??? ?System.out.println("復(fù)制成功!?。?); ?? ?}
下一篇:Java實(shí)現(xiàn)文件及文件夾的刪除
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
自定義spring mvc的json視圖實(shí)現(xiàn)思路解析
這篇文章主要介紹了自定義spring mvc的json視圖的實(shí)現(xiàn)思路解析,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12java設(shè)計(jì)模式之裝飾模式詳細(xì)介紹
這篇文章主要介紹了java設(shè)計(jì)模式之裝飾模式,有需要的朋友可以參考一下2013-12-12springmvc+kindeditor文件上傳實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了springmvc+kindeditor文件上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08ShardingSphere jdbc集成多數(shù)據(jù)源的實(shí)現(xiàn)步驟
本文主要介紹了ShardingSphere jdbc集成多數(shù)據(jù)源的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10SpringBoot使用Validation進(jìn)行參數(shù)校驗(yàn)的示例詳解
在 SpringBoot項(xiàng)目開發(fā)中,有一個(gè)觀點(diǎn)是不要相信前端傳入的參數(shù),因?yàn)槟悴恢烙脩羰窃趺床僮魑覀兘涌诘?,所以在后端也需要?duì)參數(shù)進(jìn)行校驗(yàn),這篇文章主要講講我們項(xiàng)目中最常使用的驗(yàn)證方案2023-05-05springboot RESTful以及參數(shù)注解的使用方式
這篇文章主要介紹了springboot RESTful以及參數(shù)注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10