java递归实现复制一个文件夹下所有文件功能

所属分类: 软件编程 / java 阅读数: 40
收藏 0 赞 0 分享

今天开始学习java的IO,学了一个复制文件的例子程序后想自己实现以下如何复制一个文件夹,复制文件的例子程序如下:

package io.github.liuzhan214;
import java.io.File;
import java.io.IOException;

public class Main {

 void solve() {
  File file = new File("F:\\javaIOTest\\new.txt");
  if(!file.exists()) {
   try {
    file.createNewFile();
   } 
   catch(IOException e) {
    e.printStackTrace();
   }
  }
  else {
   System.out.println(file.getName());
  }
  File dir = new File("F:\\javaIOTest");
  System.out.println(dir.getName());
  if(dir.isDirectory()) {
   String[] filename = dir.list();
   for(String str: filename) {
    File tempFile = new File(dir.getPath() + File.separator + str);
    if(tempFile.isFile()) {
     System.out.println("File: " + tempFile.getPath());
    }
    else if(tempFile.isDirectory()) {
     System.out.println("Dir: " + tempFile.getPath());
    }
   }
  }
 }
 public static void main(String[] args) {
  Main main = new Main();
  main.solve();
 }
}

createNewFile可以创建一个文件
mkdir()可以创建一个目录
FileInputStream和FileOutoutStream可以实现文件的复制

实现文件夹复制的代码如下

package io.github.liuzhan214;

import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main {

 Scanner scanner = new Scanner(System.in);

 void solve() {
  String inputString = scanner.next();
  String outputString = scanner.next();
  File inputFile = new File(inputString);
  File outputFile = new File(outputString);
  if(inputFile.exists() == false || outputFile.exists() == false) {
   System.out.println("输入或者输出路径不合法!"); return;
  }
  File sourceFile = inputFile;
  File destFile = new File(outputFile.getPath() + File.separator + inputFile.getName());
  try {
   myCopy(sourceFile,destFile);
  } catch(Exception e) {
   System.out.println("复制出现错误!"); return;
  }
  System.out.println(sourceFile.getPath() + "->" + destFile.getPath() + "复制成功!"); 
 }

 void myCopy(File sourceFile,File destFile) throws Exception{
  //保证sourceFile一定存在,destFile一定不存在,因此先创建destFile
  if(sourceFile.isFile()) destFile.createNewFile();
  else if(sourceFile.isDirectory()) destFile.mkdir();

  if(sourceFile.isFile()) copy(sourceFile,destFile);
  else if(sourceFile.isDirectory()) {
   File[] files = sourceFile.listFiles();
   for(File ele : files) {
    File newDestFile = new File(destFile.getPath() + File.separator + ele.getName());
    myCopy(ele,newDestFile);
   }
  }
 }
 void copy(File sourceFile,File destFile) throws Exception{
  //保证sourceFile一定存在,destFile一定存在
  FileInputStream inputStream = new FileInputStream(sourceFile);
  FileOutputStream outputStream = new FileOutputStream(destFile);
  byte[] arr = new byte[256];
  int len = 0;
  while((len = inputStream.read(arr)) > 0) {
   outputStream.write(arr, 0, len);
  }
  inputStream.close();
  outputStream.close();
 }

 void close() {
  scanner.close();
 }
 public static void main(String[] args) {
  Main main = new Main();
  main.solve();
  main.close();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多