package io;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner; public class copy { public static void main(String[] args) { Scanner sc = new Scanner(System.in);// 键盘输入 System.out.print("请输入需要复制的源:");
String srcPath = sc.next();
System.out.print("请输入需要复制的目的:");
String destPath = sc.next(); System.out.println("复制开始");
copys(srcPath, destPath);
System.out.println("复制结束"); } /**
* 复制
*
* @param srcPath
* 源
* @param destPath
* 目的
*/
private static void copys(String srcPath, String destPath) { File srcFile = new File(srcPath); if (srcFile.isDirectory()) {// 判断是否为文件夹 File[] files = srcFile.listFiles();// 获取文件列表,File对象 for (File file : files) { String name = File.separator + file.getName();// 获取适合计算机系统的地址分隔符;获取文件名或文件夹名 if (file.isDirectory()) {// 判断是否为文件夹
copyDirectory(srcPath + name, destPath + name);
} if (file.isFile()) {// 判断是否为文件
copyFile(file, srcPath + name, destPath + name);
} } } if (srcFile.isFile()) {// 判断是否为文件
copyFile(srcFile, srcPath, destPath);
} } /**
* 复制文件夹
*
* @param srcPath
* 源
* @param destPath
* 目的
*/
private static void copyDirectory(String srcPath, String destPath) { File destFile = new File(destPath); if (!destFile.exists()) {// 判断文件或文件夹是否存在
destFile.mkdirs();// 创建目录(包括父目录)
} copys(srcPath, destPath); } /**
* 复制文件
*
* @param file
* File对象
* @param srcPath
* 源
* @param destPath
* 目的
*/
private static void copyFile(File file, String srcPath, String destPath) { InputStream is = null;
OutputStream os = null; File srcFile = new File(srcPath);
File destFile = new File(destPath); try { is = new FileInputStream(srcFile);
os = new FileOutputStream(destFile); int length = (int) file.length();// 获取文件的字节长度
byte[] b = new byte[length]; is.read(b);
os.write(b); } catch (IOException e) { System.out.println("IO异常, 复制失败");
e.printStackTrace(); } finally { try { os.close();
is.close(); } catch (IOException e) { System.out.println("IO异常, 复制失败");
e.printStackTrace(); } } } }

IO流 复制文件及文件夹的更多相关文章

  1. IO流的应用_Copy文件

    IO流的应用_Copy文件 (1) import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  2. io流(io流的引入与文件字节流)

    io流的引入与文件字节流 io流:就是一根吸管,插入后,可以操作目标文件 io流的分类: 按方向:输入,输出 按大小:字节,字符 按处理方式: 处理流:"管套着管" --- 流结合 ...

  3. IO流-复制多极文件夹(递归实现)

    package com.io.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  4. java用字符io流复制文件

    一.小文件一次快速读写 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...

  5. IO流——File类(文件流类)

    java语言的输入输出操作是借助于输入输出包java.io来实现的,按传输方向分为输入流与输出流,从外设传递到应用程序的流为输入流,将数据从应用程序输入到外设的流为输出流. File类的构造方法: 1 ...

  6. IO流实现简单的文件的剪切

    思路: 判断 即将 复制的文件是文件夹还是文件 遍历需要复制的源文件夹 如果是文件夹,就通过流创建一个同样的子文件夹 如果是文件,就复制过去 接下来上代码 public class Demo1 { p ...

  7. 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝

    两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...

  8. IO流实现GBK写入文件然后转换UTF-8

    public static void main(String[] args) throws IOException { File file = new File("olol\\a.txt&q ...

  9. java IO流复制图片

    一.使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // Str ...

随机推荐

  1. iOS开发线程之NSThread

    1.初始化 - (instancetype)init API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) NS_DESIGNAT ...

  2. csdn的富文本编辑器(html)格式的文章,添加的代码格式粘贴到项目中总是不对,我这里是maven的pom文件。

    在用富文本编辑器(html)格式编辑的时候,总是出现将pom文件的配置从csdn粘贴到项目中发现很多红,是因为自己在设置的时候有问题. 因为我们的是pom.xml,这里原来设置的java呀或者js为了 ...

  3. Activity详解一 配置、启动和关闭activity转载 https://www.cnblogs.com/androidWuYou/p/5887726.html

    先看效果图: Android为我们提供了四种应组件,分别为Activity.Service.Broadcast receivers和Content providers,这些组建也就是我们开发一个And ...

  4. AtCoder ABC 129E Sum Equals Xor

    题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_e 题目大意 给定一个二进制表示的数 L,问有多少对自然数 (a, b) 满足 $a + b ...

  5. day 83 Vue学习四之过滤器、钩子函数、路由、全家桶等

    Vue学习四之过滤器.钩子函数.路由.全家桶等   本节目录 一 vue过滤器 二 生命周期的钩子函数 三 vue的全家桶 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 Vue的过滤 ...

  6. tensorflow识别验证码(2)-tensorflow 编写CNN 识别验证码

    1. 导入依赖包 #coding:utf-8 from gen_captcha import gen_captcha_text_and_image from gen_captcha import nu ...

  7. vue中 给router-view 组件的 绑定 key 的原因

    不设置 router-view 的 key 属性 由于 Vue 会复用相同组件, 即 /page/1 => /page/2 或者 /page?id=1 => /page?id=2 这类链接 ...

  8. csscomb配置文件说明

    { "always-semicolon": true, // 总是显示分号 "block-indent": " ", // 代码块缩进,可以 ...

  9. Miler-Rabbin素数判定

    前言 素数判定? 小学生都可以打的出来! 直接暴力O(n)O(\sqrt n)O(n​)-- 然后就会发现,慢死了-- 于是我们想到了筛法,比如前几天说到的詹欧筛法. 但是线性的时间和空间成了硬伤-- ...

  10. Less适配移动端rem

    @ue-width: 750; /* 设计图的宽度 */ .px2rem(@px) { @remValue: @px/@ue-width*10; @pxToRem: ~"@{remValue ...