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)文章
eclipse如何運(yùn)行springboot項(xiàng)目
這篇文章主要介紹了eclipse如何運(yùn)行springboot項(xiàng)目問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Spring Boot中使用@Scheduled和Quartz實(shí)現(xiàn)定時(shí)任務(wù)的詳細(xì)過程
Spring Boot提供了兩種常見的定時(shí)任務(wù)實(shí)現(xiàn)方式,一種是基于@Scheduled注解的簡(jiǎn)單定時(shí)任務(wù),另一種是功能更強(qiáng)大的Quartz框架,本文將介紹如何在Spring Boot中使用這兩種方式實(shí)現(xiàn)定時(shí)任務(wù)和調(diào)度功能,感興趣的朋友一起看看吧2025-08-08
詳談ThreadLocal-單例模式下高并發(fā)線程安全
這篇文章主要介紹了ThreadLocal-單例模式下高并發(fā)線程安全,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring Boot 單元測(cè)試和集成測(cè)試實(shí)現(xiàn)詳解
這篇文章主要介紹了Spring Boot 單元測(cè)試和集成測(cè)試實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
spring-kafka使消費(fèi)者動(dòng)態(tài)訂閱新增的topic問題
這篇文章主要介紹了spring-kafka使消費(fèi)者動(dòng)態(tài)訂閱新增的topic問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Netty學(xué)習(xí)之理解selector原理示例
這篇文章主要為大家介紹了Netty學(xué)習(xí)之理解selector原理示例使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-07-07
Java 語言實(shí)現(xiàn)清除帶 html 標(biāo)簽的內(nèi)容方法
下面小編就為大家?guī)硪黄狫ava 語言實(shí)現(xiàn)清除帶 html 標(biāo)簽的內(nèi)容方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

