修改后的CopyFile类
package com.test; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* 通过一定的逻辑判断具体调用哪个方法
* @author Dawn
*
*/
public class CopyFile {
/**
* 在同目录下拷贝文件,加上文件名的命名逻辑
* @param srcFilePath 源文件路径
* @param destFilePath 目标文件路径
*/
public static void copyFileSamePath(String srcFilePath,String destFilePath){
File srcFile=new File(srcFilePath);
File destFile=new File(destFilePath);
if(!srcFile.exists()){
System.out.println("源文件不存在");
return;
}
if(!(srcFile.getPath().equals(destFile.getPath()))){
System.out.println("方法调用错误:只复制同一个目录下的文件");
return;
}
if(!srcFile.isFile()){
System.out.println("方法调用错误:源文件不是单个的文件");
return;
}
File newFile=naming(destFile);
try{
newFile.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
copyData(srcFile,newFile);
}
/**
* 用来给同目录下的新建的文件和文件夹命名
* @param destFile 目标File类的引用
* @return 返回更名后的File类的引用
*/
private static File naming(File destFile){
File newFile=null;
int increasing=2;
String folder=destFile.getParent();
String fileName="复件"+destFile.getName();
String newPath=folder+File.separator+fileName;
newFile=new File(newPath);
while(newFile.exists()){
fileName="复件"+increasing++ +" "+destFile.getName();
newPath=folder+File.separator+fileName;
newFile=new File(newPath);
}
return newFile;
}
/**
* 用来传输数据
* @param srcFile 源文件的File类引用
* @param destFile 目标文件的File类引用
*/
private static void copyData(File srcFile,File destFile){
try{
FileInputStream reader=new FileInputStream(srcFile);
FileOutputStream writer=new FileOutputStream(destFile);
int length=0;
byte[] dataBytes=new byte[4096];
while((length=reader.read(dataBytes))!=-1){
writer.write(dataBytes,0,length);
}
reader.close();
writer.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
/**
* 在不同目录下拷贝文件
* @param srcFilePath 源文件路径
* @param destFilePath 目标文件路径
* @param overlay 是否覆盖
*/
public static void copyFileDifferentPath(String srcFilePath,String destFilePath,boolean overlay){
File srcFile=new File(srcFilePath);
File destFile=new File(destFilePath);
if(!srcFile.exists()){
System.out.println("源文件不存在");
return;
}
if(srcFile.getPath().equals(destFile.getPath())){
System.out.println("方法调用错误:只能复制到不同的目录下");
return;
}
if(!srcFile.isFile()){
System.out.println("方法调用错误:只能复制文件");
return;
}
if(!(srcFile.getName().equals(destFile.getName()))){
System.out.println("输入错误:文件名不一样");
return;
}
if(destFile.exists()){
if(overlay){
destFile.delete();
}else{
System.out.println("文件重名");
return;
}
}
try{
destFile.createNewFile();
}catch(IOException e){
e.printStackTrace();
return;
}
copyData(srcFile,destFile);
}
/**
* 在不同的目录下拷贝文件夹
* @param srcFolderPath 源文件夹路径
* @param destFolderPath 目标文件夹路径
* @param overlay 是否覆盖
*/
public static void copyFolderDifferentPath(String srcFolderPath,String destFolderPath,boolean overlay){
File srcFolder=new File(srcFolderPath);
File destFolder=new File(destFolderPath);
if(!srcFolder.exists()){
System.out.println("源文件不存在");
return;
}
if(srcFolder.getPath().equals(destFolder.getPath())){
System.out.println("方法调用错误:只能复制到不同的目录下");
return;
}
if(!srcFolder.isDirectory()){
System.out.println("方法调用错误:只能复制文件夹");
return;
}
if(!(srcFolder.getName().equals(destFolder.getName()))){
System.out.println("输入错误:文件名不一样");
return;
}
if(destFolder.exists()){
if(overlay){
File[] files=srcFolder.listFiles();
int size=files.length;
for(int i=0;i<size;i++){
if(files[i].isFile()){
copyFileDifferentPath(files[i].getPath(),destFolder.getPath()+File.separator+files[i].getName(),true);
}else{
copyFolderDifferentPath(files[i].getPath(),destFolder.getPath()+File.separator+files[i].getName(),true);
}
}
}else{
System.out.println("文件夹重名");
return;
}
}else{
destFolder.mkdirs();
copyFolderDifferentPath(srcFolderPath,destFolderPath,true);
}
}
public static void copyFolderSamePath(String srcFolderPath,String destFolderPath){
File srcFolder=new File(srcFolderPath);
File destFolder=new File(destFolderPath);
if(!srcFolder.exists()){
System.out.println("源文件夹不存在");
return;
}
if(!(srcFolder.getPath().equals(destFolder.getPath()))){
System.out.println("方法调用错误:只复制同一个目录下的文件夹");
return;
}
if(!srcFolder.isDirectory()){
System.out.println("方法调用错误:源文件不是单个的文件夹");
return;
}
File newFolder=naming(destFolder);
newFolder.mkdirs();
File[] files=srcFolder.listFiles();
int size=files.length;
for(int i=0;i<size;i++){
if(files[i].isFile()){
copyFileDifferentPath(files[i].getPath(),newFolder.getPath()+File.separator+files[i].getName(),false);
}else{
copyFolderDifferentPath(files[i].getPath(),newFolder.getPath()+File.separator+files[i].getName(),false);
}
} }
/**
* 测试用main方法
* @param args
*/
public static void main(String[] args){
copyFileSamePath("F:\\World of Warcraft\\Data\\data\\data.019","F:\\World of Warcraft\\Data\\data\\data.019");
}
} //删除功能,配合上面的CopyFile可以达到剪切效果
package folderoperation; import java.io.File;
/**
* 注意它会删除文件,文件夹以及文件夹下的所有内容(根据指定的地址)
* @author Dawn
*
*/
public class DeleteFolder {
/**
* 删除文件的方法
* @param filePath 文件或文件夹的地址
* @return
*/
public static boolean delete(String filePath){
File file=new File(filePath);
if(file.exists()){
if(file.isFile()){
file.delete();
return true;
}else{
File[] files=file.listFiles();
int size=files.length;
for(int i=0;i<size;i++){
DeleteFolder.delete(files[i].getPath());
}
file.delete();
return true;
}
}else{
System.out.println("文件或文件夹不存在");
return false;
}
}
public static void main(String[] args){
delete("/Users/Dawn/Documents/JavaPractice/dataFile.txt");
}
}
修改后的CopyFile类的更多相关文章
- 将JAR包反编译,修改后重新打包(转)
将JAR包反编译,修改后重新打包(转) 在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的 ...
- P141 实战练习——字符串(修改后)
1.在项目中创建Number类,判断字符串“mingrikejijavabu”中字符‘i’出现了几次,并将结果输出. 方法一: // String str="mingrikejijavabu ...
- IDEA运行编译后配置文件无法找到,或配置文件修改后无效的问题
1.触发事件 今天正好在学习log4j,为了测试其配置文件log4j.properties中的各种配置,进行了频繁修改和程序启动以确认效果,因为是使用的IDEA建立的Web项目,接着问题就来了,配置文 ...
- java 记录对象前后修改的内容(工具类)
有时候业务需要,需记录一条记录的修改历史,但是不能为完成任务而硬编码,不靠谱 这种情况可以使用java反射来完成 对对象属性的描述可以通过自定义注解来完成,读取里面的属性进而记录修改历史. 在对象的属 ...
- eclipse 使用tomcat运行JavaWeb项目,文件修改后为何不用重启tomcat? (运行web项目的4种方式)探究
1.情景说明 在eclipse中,为什么Java文件修改后,重启tomcat class文件才能生效? 为什么jsp修改后,不需重启tomcat就能立即生效? 为什么静 ...
- spring boot注解 --@spring-boot-devtools 自动加载修改的文件和类
spriing boot中有一个注解,是自动加载修改后的类或者文件. 使用方法为: spring-boot-devtools=true 需要引入devtools包依赖: <dependency& ...
- static final修饰的静态变量修改后更新到服务器,重启无法生效的问题
今天在工作中碰到这样一个问题,有一个常量类,将工程中常用的一些变量定义在了里面.今天我要修改其中的某个变量.修改完后将编译好的.class文件更新到了服务器上,但是重启服务器后发现始终没有变化,还是以 ...
- Jar包进行反编译,修改后重新打包
在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的使用说明和源代码,有时有的却不提供源代码,使用 ...
- SpringBoot集成MybatisPlus解决Mapper文件修改后动态刷新的问题
很多人在使用SpringBoot集成Mybatis或者MybatisPlus的时候在查询复杂的情况下会写mapper文件,虽然说MyBatisPlus提供了常用的增删查改,但还是难以应付复杂的查询.关 ...
随机推荐
- HDU 1166 敌兵布阵 (树状数组)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) ...
- Windows程序内部运行机制 转自http://www.cnblogs.com/zhili/p/WinMain.html
一.引言 要想熟练掌握Windows应用程序的开发,首先需要理解Windows平台下程序运行的内部机制,然而在.NET平台下,创建一个Windows桌面程序,只需要简单地选择Windows窗体应用程序 ...
- .NET面试题目
简单介绍下ADO.NET和ADO主要有什么改进? 答:ADO以Recordset存储,而ADO.NET则以DataSet表示,ADO.NET提供了数据集和数据适配器,有利于实现分布式处理,降低了对数据 ...
- 随鼠标轮动翻动层————jquery小练习
闲来无事在网站上看见一个网页制作的不错,就仿照做来看看.特此记录下来. 亮点:随鼠标上下滚动,展示页面随之不同,翻动效果. 功能点:鼠标向上,向下判断事件. css 代码 html { overflo ...
- java实现图像灰度化
/*在研究Java实现将一张图片转成字符画的时候,发现将图像转化字符串是根据照片的灰度采用不同的字符画出来,形成一个灰度表.于是就研究了下关于灰度值这个东西,于是跳了一个大坑...因为鄙人用的ubun ...
- spring mvc异常统一处理(ControllerAdvice注解)
首先我的项目是一个为移动端提供的json数据的,当后台报错时如果为移动端返回一个错误页面显得非常不友好,于是通过ControllerAdvice注解返回json数据. 首先创建一个异常处理类: pac ...
- 总结-EL表达式
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ tagl ...
- 带你玩转JavaWeb开发之四 -如何用JS做登录注册页面校验
今日内容 使用JQuery完成页面定时弹出广告 使用JQuery完成表格的隔行换色 使用JQuery完成复选框的全选效果 使用JQuery完成省市联动效果 使用JQuery完成下列列表左右选择 使用J ...
- php数字索引数组去重及恢复索引
$tmp = array('a','b','c','a'); $tmp = array_values(array_unique($tmp)); print_r($tmp);exit; //输出 Arr ...
- android内存分析:heap Snapshot的使用
网上有很多讲解关于android studio中memory工具的使用,接下来我来说一段在项目中发生的实例:大家可以根据我的这个方法来分析自己项目中的问题 首先我们要通过手动先触发GC操作,点击mem ...