Java的IO流實現(xiàn)文件和文件夾的復制
本文實例為大家分享了Java的IO流實現(xiàn)文件和文件夾復制的具體代碼,供大家參考,具體內(nèi)容如下
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文件夾下已經(jīng)存在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ù)調(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("復制成功?。?!");
?? ?}以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot中使用@Scheduled和Quartz實現(xiàn)定時任務的詳細過程
Spring Boot提供了兩種常見的定時任務實現(xiàn)方式,一種是基于@Scheduled注解的簡單定時任務,另一種是功能更強大的Quartz框架,本文將介紹如何在Spring Boot中使用這兩種方式實現(xiàn)定時任務和調(diào)度功能,感興趣的朋友一起看看吧2025-08-08
詳談ThreadLocal-單例模式下高并發(fā)線程安全
這篇文章主要介紹了ThreadLocal-單例模式下高并發(fā)線程安全,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring Boot 單元測試和集成測試實現(xiàn)詳解
這篇文章主要介紹了Spring Boot 單元測試和集成測試實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
spring-kafka使消費者動態(tài)訂閱新增的topic問題
這篇文章主要介紹了spring-kafka使消費者動態(tài)訂閱新增的topic問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Java 語言實現(xiàn)清除帶 html 標簽的內(nèi)容方法
下面小編就為大家?guī)硪黄狫ava 語言實現(xiàn)清除帶 html 標簽的內(nèi)容方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

