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类的更多相关文章

  1. 将JAR包反编译,修改后重新打包(转)

     将JAR包反编译,修改后重新打包(转)   在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的 ...

  2. P141 实战练习——字符串(修改后)

    1.在项目中创建Number类,判断字符串“mingrikejijavabu”中字符‘i’出现了几次,并将结果输出. 方法一: // String str="mingrikejijavabu ...

  3. IDEA运行编译后配置文件无法找到,或配置文件修改后无效的问题

    1.触发事件 今天正好在学习log4j,为了测试其配置文件log4j.properties中的各种配置,进行了频繁修改和程序启动以确认效果,因为是使用的IDEA建立的Web项目,接着问题就来了,配置文 ...

  4. java 记录对象前后修改的内容(工具类)

    有时候业务需要,需记录一条记录的修改历史,但是不能为完成任务而硬编码,不靠谱 这种情况可以使用java反射来完成 对对象属性的描述可以通过自定义注解来完成,读取里面的属性进而记录修改历史. 在对象的属 ...

  5. eclipse 使用tomcat运行JavaWeb项目,文件修改后为何不用重启tomcat? (运行web项目的4种方式)探究

                    1.情景说明 在eclipse中,为什么Java文件修改后,重启tomcat class文件才能生效? 为什么jsp修改后,不需重启tomcat就能立即生效? 为什么静 ...

  6. spring boot注解 --@spring-boot-devtools 自动加载修改的文件和类

    spriing boot中有一个注解,是自动加载修改后的类或者文件. 使用方法为: spring-boot-devtools=true 需要引入devtools包依赖: <dependency& ...

  7. static final修饰的静态变量修改后更新到服务器,重启无法生效的问题

    今天在工作中碰到这样一个问题,有一个常量类,将工程中常用的一些变量定义在了里面.今天我要修改其中的某个变量.修改完后将编译好的.class文件更新到了服务器上,但是重启服务器后发现始终没有变化,还是以 ...

  8. Jar包进行反编译,修改后重新打包

    在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的使用说明和源代码,有时有的却不提供源代码,使用 ...

  9. SpringBoot集成MybatisPlus解决Mapper文件修改后动态刷新的问题

    很多人在使用SpringBoot集成Mybatis或者MybatisPlus的时候在查询复杂的情况下会写mapper文件,虽然说MyBatisPlus提供了常用的增删查改,但还是难以应付复杂的查询.关 ...

随机推荐

  1. java 编码

    编码和解码:无论是解码还是编码都是针对码表来讲的. 编码:将能看懂得字符变为我们看不懂的码值. 字符串--->字节数组 String类的getBytes() 方法进行编码,将字符串,转为对映的二 ...

  2. Javascript初学篇章_3(注释/if/switch)

    五.javascript的注释 // 单行注释 /* …*/ 多行注释 注:多行注释不能互相嵌套 六.程序流程控制 一.条件语句 if 语法: if(condition) statements1 el ...

  3. union

    union是C++中一个类,平时自己用的比较少,了解了一些特性后觉得在内存要求比较高的地方还是大有可为,总结一下. C++中的struct和union内部都可以定义 数据 和 函数, 两个跟 clas ...

  4. java工程师 学习路线图

  5. php套件 wampserver 常见问题

    安装问题 dll 丢失 今天手贱升级了win10,重新安装了一下php的套件,提示有几个组件找不到,其中有一个msvcr100.dll丢失. google了一下: youtube视频:Windows ...

  6. C#组合查询小Demo

    namespace WindowsFormsApplication1 { public partial class Form1 : Form { string Sql = "select * ...

  7. 谈谈对Spring IOC的理解【转】

    学习过Spring框架的人 一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大 ...

  8. 转:PostgreSQL Cheat Sheet

    PostgreSQL Cheat Sheet CREATE DATABASE CREATE DATABASE dbName; CREATE TABLE (with auto numbering int ...

  9. HTML第二本书学习后记

    1,换行标记<br>,不换行标记<nobr></nobr> 2,添加水平线 <hr> 3,插入特殊符号:   空格 &nbsp    " ...

  10. Mac上安装django

    参考:https://docs.djangoproject.com/en/1.9/topics/install/#installing-official-release 升级pip sudo pip ...