java将一个目录下的所有数据复制到另一个目录下
作者:wangtianze 发布时间:2023-01-08 15:11:44
标签:java,数据复制
本文实例为大家分享了java将一个目录下的所有数据复制到另一个目录下的具体代码,供大家参考,具体内容如下
/*
将"C:\\JavaProducts\\Source"下的所有数据复制到"C:\\Target"下
*/
import java.io.*;
public class JavaCopyDemo{
final static String SOURCESTRING = "C:\\JavaProducts\\Source";
final static String TARGETSTRING = "C:\\Target";
public static void main(String[] args){
if(!(new File(SOURCESTRING)).exists()){
System.out.println("源文件" + SOURCESTRING + "不存在,无法复制!");
return;
}else if((new File(TARGETSTRING)).exists()){
System.out.println("目标文件" + TARGETSTRING + "已经存在,无法复制!");
return;
}else{
if((new File(SOURCESTRING)).isFile()){
copyFile(new File(SOURCESTRING),new File(TARGETSTRING));
}else if((new File(SOURCESTRING)).isDirectory()){
copyDirectory(SOURCESTRING,TARGETSTRING);
}
}
}
private static void copyFile(File sourceFile,File targetFile){
if(!sourceFile.canRead()){
System.out.println("源文件" + sourceFile.getAbsolutePath() + "不可读,无法复制!");
return;
}else{
System.out.println("开始复制文件" + sourceFile.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try{
fis = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(targetFile);
bos = new BufferedOutputStream(fos);
int len = 0;
while((len = bis.read()) != -1){
bos.write(len);
}
bos.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fis != null){
fis.close();
}
if(bis != null){
bis.close();
}
if(fos != null){
fos.close();
}
if(bos != null){
bos.close();
}
System.out.println("文件" + sourceFile.getAbsolutePath() + "复制到" + targetFile.getAbsolutePath() + "完成");
}catch(IOException e){
e.printStackTrace();
}
}
}
}
private static void copyDirectory(String sourcePathString,String targetPathString){
if(!new File(sourcePathString).canRead()){
System.out.println("源文件夹" + sourcePathString + "不可读,无法复制!");
return;
}else{
(new File(targetPathString)).mkdirs();
System.out.println("开始复制文件夹" + sourcePathString + "到" + targetPathString);
File[] files = new File(sourcePathString).listFiles();
for(int i = 0; i < files.length; i++){
if(files[i].isFile()){
copyFile(new File(sourcePathString + File.separator + files[i].getName()),new File(targetPathString + File.separator + files[i].getName()));
}else if(files[i].isDirectory()){
copyDirectory(sourcePathString + File.separator + files[i].getName(),targetPathString + File.separator + files[i].getName());
}
}
System.out.println("复制文件夹" + sourcePathString + "到" + targetPathString + "结束");
}
}
}
来源:https://www.cnblogs.com/wangtianze/p/6690652.html


猜你喜欢
- 本文实例讲述了Java对象数组定义与用法。分享给大家供大家参考,具体如下:所谓的对象数组,就是指包含了一组相关的对象,但是在对象数组的使用中
- 一、原理1、不变模式(不可变对象)在并行软件开发过程中,同步操作似乎是必不可少的。当多线程对同一个对象进行读写操作时,为了保证对象数据的一致
- 起因unity程序build到pc上,拿到其他人的机器上结果有些功能不正常,看log里面大概是Fallback handler could
- 本文实例为大家分享了java实现时间与字符串之间转换的具体代码,供大家参考,具体内容如下1. long字符串转换成yyyy-MM-dd HH
- 一、实现流程1.注册2.登录3.登录保持【状态续签】二、实现方法项目结构1.引入依赖<!-- spring-web --><
- 一、背景今天心血来潮,准备测试一下项目中 logback 的自动刷新功能,但是测试时发现并不生效。logback 的配置如下:<con
- 在很多.net开发体系中开发者在面对调度作业需求的时候一般会选择三方开源成熟的作业调度框架来满足业务需求,比如Hangfire、Quartz
- Volatile关键字的作用主要有如下两个:1.线程的可见性:当一个线程修改一个共享变量时,另外一个线程能读到这个修改的值。2. 顺序一致性
- 最近写了一个简单的聊天室应用,可以发送表情,更改头像这些功能。主要技术点就是怎样把表情图片放到textview等Ui控件中展示。这里废话不多
- 最长公共子序列(Longest Common Subsequence)定义:两个或多个已知数列的子序列集合中最长的就是最长公共子序列。其实说
- 本文实例讲述了Java常用内置注解。分享给大家供大家参考,具体如下:一 通过@SuppressWarnings关闭警告信息1 代码publi
- 本文实例为大家分享了java实现在线聊天系统的具体代码,供大家参考,具体内容如下本博客是博主在观看相关视频后写下的代码,希望能够帮助大家掌握
- 使用Spring data JPA开发已经有一段时间了,这期间学习了一些东西,也遇到了一些问题,在这里和大家分享一下。前言:Spring d
- 1.springboot 2.0 默认连接池就是Hikari了,所以引用parents后不用专门加依赖2.贴我自己的配置(时间单位都是毫秒)
- 本文实例讲述了Android编程之交互对话框。分享给大家供大家参考,具体如下:1. 在Android SDK中,虽然有许多的窗口,有些类似M
- 几点重要的用法:a 先来介绍几个方法TimeSpan.Minutes(其他时间比如天数,小时数,秒数都一样的情况下得到的分钟数的差),其他的
- 一、什么是ImportBeanDefinitionRegistrarImportBeanDefinitionRegistrar接口是也是sp
- 1、Android内存管理机制1.1 Java内存分配模型先上一张JVM将内存划分区域的图程序计数器:存储当前线程执行目标方法执行到第几行。
- 前言在开始介绍socket前先补充补充基础知识,在此基础上理解网络通信才会顺理成章,当然有基础的可以跳过去了。都是废话,进入正题。TCP/I
- 对某个类型中的方法进行拦截,然后加入固定的业务逻辑,这是AOP面向切面编程可以做的事,在springboot里实现aop的方法也有很多, s