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. Android 双卡双待识别

    简介 Android双卡双待已经越来越普及了,解决双卡双待管理是广大手机开发人员必须得面对的问题,为实现Android平台的双卡双待操作,笔者研究了Android 应用层操作双卡双待的机制. 机制 获 ...

  2. ADT Ubuntu X64 下ia32-libs替换等【待编辑】

    sudo apt-get install ia32-libs apt-get install libglib2.0-0:i386 libpng12-0:i386 libsm6:i386 libxren ...

  3. 2016huasacm暑假集训训练三 F - Jungle Roads

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/F 题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使 ...

  4. css 温故而知新 slideDown/slideUp 新思路

    默认设置高度为0; -webkit-transition:.3s all ease;transition:.3s all ease;opacity:0;height:0; 需要时添加高度即可 heig ...

  5. delphi URL 编码的转换

    先介绍一下,Delphi中处理Google的URL编码解码,其中就会明白URL编码转换的方法的 从delphi的角度看Google(谷歌)URL编码解码方式 在网上搜索了一下,似乎没有什么关于goog ...

  6. mytatis将Integer等于0识别成空字符串

    在进行myBatis条件查询的时候,会有如下操作: <if test="delFlag !=null and delFlag != ''"> and t.del_fla ...

  7. static{ }语句块详解

    static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法.举ge例子: public class Test { public static i ...

  8. linux添加环境变量(centos)

    1.查看当前环境变量 #echo $PATH 2.增加环境变量 #vi /etc/profile export PATH=/usr/path/bin:$PATH 3.生效 #source /etc/p ...

  9. scala集合List和Set

    一:List集合 1.创建 2.简单使用(两个部分) 3.Nill空集合 4.创建一个可变的list集合 二:Set 1.说明 无序,不重复 2.新建 3.可变

  10. [flex布局]-flex教程

    简介:2009年,W3C提出了一种新的方案----Flex布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能. Flex布局是什 ...