IO流 复制文件及文件夹
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流 复制文件及文件夹的更多相关文章
- IO流的应用_Copy文件
IO流的应用_Copy文件 (1) import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...
- io流(io流的引入与文件字节流)
io流的引入与文件字节流 io流:就是一根吸管,插入后,可以操作目标文件 io流的分类: 按方向:输入,输出 按大小:字节,字符 按处理方式: 处理流:"管套着管" --- 流结合 ...
- IO流-复制多极文件夹(递归实现)
package com.io.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- java用字符io流复制文件
一.小文件一次快速读写 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...
- IO流——File类(文件流类)
java语言的输入输出操作是借助于输入输出包java.io来实现的,按传输方向分为输入流与输出流,从外设传递到应用程序的流为输入流,将数据从应用程序输入到外设的流为输出流. File类的构造方法: 1 ...
- IO流实现简单的文件的剪切
思路: 判断 即将 复制的文件是文件夹还是文件 遍历需要复制的源文件夹 如果是文件夹,就通过流创建一个同样的子文件夹 如果是文件,就复制过去 接下来上代码 public class Demo1 { p ...
- 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝
两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...
- IO流实现GBK写入文件然后转换UTF-8
public static void main(String[] args) throws IOException { File file = new File("olol\\a.txt&q ...
- java IO流复制图片
一.使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // Str ...
随机推荐
- Android Drawable 详解(教你画画!)
参考 1.Android中的Drawable基础与自定义Drawable 2.android中的drawable资源 3.Android开发之Shape详细解读 Drawable分类 No xml标签 ...
- Eclipse 连接MySql数据库总结
Eclipse 连接MySql数据库总结 一.在MySql中创建数据库,并创建表,向表中插入数据 1.创建数据库 create database select_test 2.创建表 create ta ...
- springcloud Finchley 版本hystrix 和 hystrix Dashboard
hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...
- nginx源码分析-源码结构
本文主要简单介绍nginx源码目录结构.程序编译流程.如何构建学习nginx的环境等.本文以及后续nginx源码分析文章是基于nginx当前(2009-02-27)的稳定版本0.6.35进行的分析,该 ...
- springboot整合jpa和mybatis实现主从复制
百度多方参考终于配出我自己的了,以下仅供参考 参考https://www.cnblogs.com/cjsblog/p/9712457.html 代码 首先数据源配置 spring.datasource ...
- 2019-8-31-dotnet-获取程序所在路径的方法
title author date CreateTime categories dotnet 获取程序所在路径的方法 lindexi 2019-08-31 16:55:58 +0800 2019-03 ...
- opencv编译:opencv 3.4.1 编译 contrib模块,增加人脸识别
start cmake-gui select the opencv source code folder and the folder where binaries will be built (th ...
- [JZOJ5355] 【NOIP2017提高A组模拟9.9】保命
题目 描述 题目已经足够清晰了,所以不再赘述题目大意. 思考历程 一眼看下去,好像是一道大水题! 然而,再看几眼,感觉又不是一道水题! 然后想了半天,感觉它特别难转移! 最终打了一个暴力,然后发现样例 ...
- F. Cowmpany Cowmpensation dp+拉格朗日插值
题意:一个数,每个节点取值是1-d,父亲比儿子节点值要大,求方案数 题解:\(dp[u][x]=\prod_{v}\sum_{i=1}^xdp[v][i]\),v是u的子节点,先预处理出前3000项, ...
- js声明变量的三种方式
JS 声明变量的三种方式 (1)使用变量步骤:a.声明-->b.赋值-->3.调用 正确用法: <script type="text/javascript"> ...