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 ...
随机推荐
- VS2010-MFC(常用控件:滚动条控件Scroll Bar)
转自:http://www.jizhuomi.com/software/191.html 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合框设置了相应属性 ...
- day23_2_logging
#!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...
- 继承关系中子类使用@Data注解问题
HashSet中使用@Data注解问题 平时习惯使用lombok工具,免去了我们写get.set方法之类的,当然了,我们使用@Data注解后,equals().hashCode().toString( ...
- [NOIP2019模拟赛]序列(Sequence)
题目大意 有一个序列$A_i$ • 对于 i ≥ 1,如果有$ A_i > 0.A_{i+1}> 0$ 且存在 $A_{i+2}$,那么法老可以令$ Ai$ 和 $A_{i+1}$ 减一, ...
- 【学术篇】树上差分--洛谷3128最大流Max Flow
懒得贴题目,直接放不稳定的传送门(雾):点击前往暴风城(雾) 据说这题是BZOJ3490,但本蒟蒻没有权限╮(╯_╰)╭ 这题似乎就是裸树上差分... 对于树上(x,y)之间的路径上的点区间c[i]加 ...
- Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F
A. Wrong Subtraction 题目大意: 定义一种运算,让你去模拟 题解: 模拟 #include <iostream> #include <cstdio> ...
- 【珍惜时间】iReport
项目很点意思,感觉很高超的样子 先放下项目的github地址:https://github.com/tctangyanan/iReport 感谢各位伟大的程序员无私的分享自己的技术 老规矩,我们会运行 ...
- nodejs vue-cli 微信公众号开发(一) 申请域名搭建服务器
一.搭建本地服务器 1.首先保存本地的80端口被node监听,利用内网穿透工具把80端口映射出去.(ngrok工具可以穿透内网使本地ip作为外网使用) 2.打开https://natapp.cn/tu ...
- springboot实现转发和重定向
1.转发 方式一:使用 "forword" 关键字(不是指java关键字),注意:类的注解不能使用@RestController 要用@Controller @Reques ...
- 【玲珑杯 round#18 A】计算几何你瞎暴力
[Link]:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 [Description] [Solution] 因为每个点的(xi ...