Java的IO流實現(xiàn)文件和文件夾的復制
本文實例為大家分享了Java的IO流實現(xiàn)文件和文件夾復制的具體代碼,供大家參考,具體內容如下
1、使用文件流對單個文件進行復制
將某個文件復制到指定的路徑下:
//復制文件 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、使用文件流將文件及文件夾下的文件進行復制
將a文件夾下的文件拷貝到b文件夾里。a和b文件夾可能存在相同文件,若b文件夾下已經存在a文件夾中的文件則不拷貝,若不存在則拷貝。
(1)遞歸復制文件夾下的文件
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中含有文件,直接復制文件 ?? ??? ??? ?else { ?? ??? ??? ??? ?copyOtherFile(new File(A.getPath()+"/"+a[i]), B); ?? ??? ??? ?}?? ??? ??? ? ?? ??? ?} ?? ??? ? ?? ?}
(2)若含有同名文件,則不進行復制
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()); ?? ??? ??? ??? ?//含有同名文件,不進行復制 ?? ??? ??? ??? ?if (srcfile.getName().equals(f2.getName())) { ?? ??? ??? ??? ??? ?System.out.println("相同"); ?? ??? ??? ??? ??? ?flag = false; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?System.out.println("不相同"); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?if (flag) { ?? ??? ??? ??? ?copy(srcfile, destfile.getPath() + "/"); ?? ??? ??? ?} ?? ??? ? ?? ?}
(3)主函數(shù)調用
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("復制成功?。。?); ?? ?}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
自定義spring mvc的json視圖實現(xiàn)思路解析
這篇文章主要介紹了自定義spring mvc的json視圖的實現(xiàn)思路解析,本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2017-12-12ShardingSphere jdbc集成多數(shù)據(jù)源的實現(xiàn)步驟
本文主要介紹了ShardingSphere jdbc集成多數(shù)據(jù)源的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10SpringBoot使用Validation進行參數(shù)校驗的示例詳解
在 SpringBoot項目開發(fā)中,有一個觀點是不要相信前端傳入的參數(shù),因為你不知道用戶是怎么操作我們接口的,所以在后端也需要對參數(shù)進行校驗,這篇文章主要講講我們項目中最常使用的驗證方案2023-05-05springboot RESTful以及參數(shù)注解的使用方式
這篇文章主要介紹了springboot RESTful以及參數(shù)注解的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10