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 Properties 配置信息类

    Properties(配置信息类):主要用于生产配置文件和读取配置文件信息. ----> 是一个集合类 继承HashTable 存值是以键-值的方式. package com.beiwo.io; ...

  2. 【整理】强化学习与MDP

    [入门,来自wiki] 强化学习是机器学习中的一个领域,强调如何基于环境而行动,以取得最大化的预期利益.其灵感来源于心理学中的行为主义理论,即有机体如何在环境给予的奖励或惩罚的刺激下,逐步形成对刺激的 ...

  3. vpn

    https://itunes.apple.com/us/app/sonicwall-mobile-connect/id822514576?mt=12

  4. CG_INLINE,inline 内联函数

    内联函数,即在编译的时候将函数体替换函数调用,从而不需要将parameter,return address进行push/pop stack的操作,从而加速app的运行,然而,会增加二进制文件的大小. ...

  5. windows平台 查看 dll 程序集 PublicKeyToken

    打开Developer Command Prompt for VS20** 命令工具 路径:点击开始->所有程序->Microsoft Visual Studio 20** ->Vi ...

  6. C#测试运行时间

    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //开始监视代码运行时间 ...

  7. oracle 参考

    create or replace function fun_try(v_name varchar,v_outname out varchar)return varchar2 is Result va ...

  8. laravel5.1学习(2)-- artisan tinker命令

    例如:为users表创建20条测试输入 G:\wamp\www\hcmf>php artisan tinker >>> namespace App; => null &g ...

  9. Java多线程代码示例

    package algorithm; class Mythread extends Thread{ String name; public Mythread(String name){ this.na ...

  10. ios cell时间相同隐藏算法