package copyfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CopyFile {
    /**
     * 如果操作字符串的话还可以实现mac的命名方式
     * 这是同文件夹下的文件复制
     * @param srcFilePath 源文件路径
     * @param destFilePath 目标文件路径
     * @return 复制成功返回true
     */
    public static boolean copyFileSamePath(String srcFilePath,String destFilePath){
        File srcFile=new File(srcFilePath);
        File destFile=new File(destFilePath);
        if(!(srcFile.exists())){
            System.out.println("源文件不存在,无法复制");
            return false;
        }
        if(!(srcFile.getPath().equals(destFile.getPath()))){
            System.out.println("此方法只能复制同一个目录下的文件");
            return false;
        }
        if(!(srcFile.isFile())){
            System.out.println("此方法只复制文件");
            return false;
        }
        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);
            
        }
        byte[] dataBytes=null;
        try{
            FileInputStream reader=new FileInputStream(srcFile);
            dataBytes=new byte[reader.available()];
            reader.read(dataBytes);
            reader.close();
        }catch(FileNotFoundException e){
            System.out.println(e.getMessage());
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
        try{
            newFile.createNewFile();
            FileOutputStream writer=new FileOutputStream(newFile);
            writer.write(dataBytes);
            writer.close();
        }catch(FileNotFoundException e){
            System.out.println(e.getMessage());
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
        if(newFile.exists()){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 这是不同文件夹下的文件复制
     * @param srcFilePath 源文件路径
     * @param destFilePath 目标路径
     * @param overlay 是否覆盖
     * @return 成功返回true
     */
    public static boolean copyFileDifferentPath(String srcFilePath,String destFilePath,boolean overlay){
        File srcFile=new File(srcFilePath);
        File destFile=new File(destFilePath);
        if(!(srcFile.exists())){
            System.out.println("源文件不存在,无法复制");
            return false;
        }
        if(srcFile.getPath().equals(destFile.getPath())){
            System.out.println("文件的目录必须不一样");
            return false;
        }
        if(!(srcFile.isFile())){
            System.out.println("此方法只复制文件");
            return false;
        }
        if(!(srcFile.getName().equals(destFile.getName()))){
            System.out.println("文件名必须一样");
            return false;
        }
        if(destFile.exists()){
            if(overlay){
                destFile.delete();
            }else{
                System.out.println("文件重名");
                return false;
            }
        }
        try{
            destFile.createNewFile();    
        }catch(IOException e){
            e.printStackTrace();
            return false;
        }
        byte[] dataBytes=null;
        try{
            FileInputStream reader=new FileInputStream(srcFile);
            dataBytes=new byte[reader.available()];
            reader.read(dataBytes);
            reader.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
        try{
            FileOutputStream writer=new FileOutputStream(destFile);
            writer.write(dataBytes);
            writer.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
        if(destFile.exists()){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 这是不同文件夹下的文件夹复制方法
     * @param srcFolderPath    源文件夹地址
     * @param destFolderPath    目标文件地址
     * @param overlay    是否覆盖,如果覆盖那么目标文件夹内的所有同名文件和文件夹都会被替换,不同名不受影响
     * @return 成功返回true
     */
    public static boolean copyFolderDifferentPath(String srcFolderPath,String destFolderPath,boolean overlay){
        File srcFile=new File(srcFolderPath);
        File destFile=new File(destFolderPath);
        if(!(srcFile.exists())){
            System.out.println("源文件不存在,无法复制");
            return false;
        }
        if(srcFile.getPath().equals(destFile.getPath())){
            System.out.println("文件的目录必须不一样");
            return false;
        }
        if(!(srcFile.getName().equals(destFile.getName()))){
            System.out.println("文件夹名必须一样");
            return false;
        }
        if(!srcFile.isDirectory()){
            System.out.println("这个方法只复制文件夹");
            return false;
        }
        if(destFile.exists()){
            if(overlay){
                File[] files=srcFile.listFiles();
                int size=files.length;
                for(int i=0;i<size;i++){
                    if(files[i].isFile()){
                        CopyFile.copyFileDifferentPath(files[i].getPath(), destFile.getPath()+File.separator+files[i].getName(), true);
                    }else{
                        CopyFile.copyFolderDifferentPath(files[i].getPath(), destFile.getPath()+File.separator+files[i].getName(), true);
                    }
                }
            }else{
                System.out.println("文件夹重名");
                return false;
            }
        }else{
            destFile.mkdirs();
            File[] files=srcFile.listFiles();
            int size=files.length;
            for(int i=0;i<size;i++){
                if(files[i].isFile()){
                    CopyFile.copyFileDifferentPath(files[i].getPath(), destFile.getPath()+File.separator+files[i].getName(), true);
                }else{
                    CopyFile.copyFolderDifferentPath(files[i].getPath(), destFile.getPath()+File.separator+files[i].getName(), true);
                }
            }
        }
        return true;
    }
    /**
     * 这是同目录下的文件夹复制方法,以创建复件的方式
     * @param srcFolderPath    源文件夹
     * @param destFolderPath    目标文件夹
     * @return    成功返回true
     */
    public static boolean copyFolderSamePath(String srcFolderPath,String destFolderPath){
        File srcFile=new File(srcFolderPath);
        File destFile=new File(destFolderPath);
        if(!(srcFile.exists())){
            System.out.println("源文件不存在,无法复制");
            return false;
        }
        if(!(srcFile.isDirectory())){
            System.out.println("此方法只复制文件夹");
            return false;
        }
        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);
        }
        newFile.mkdirs();
        File[] files=srcFile.listFiles();
        int size=files.length;
        for(int i=0;i<size;i++){
            if(files[i].isFile()){
                CopyFile.copyFileDifferentPath(files[i].getPath(), newFile.getPath()+File.separator+files[i].getName(), false);
            }else{
                CopyFile.copyFolderDifferentPath(files[i].getPath(), newFile.getPath()+File.separator+files[i].getName(), false);
            }
        }
        return true;
    }
    public static void main(String[] args){
        copyFolderSamePath("/Users/Dawn/Documents/JavaPractice/dataFile","/Users/Dawn/Documents/JavaPractice/dataFile");
    }
}

这是一个拷贝文件的类,里面有4个方法,集成了文件的所有拷贝方式,包括在同目录下拷贝文件夹和文件,拷贝文件或文件夹到不同的目录,因为直接使用的输出流和输入流操作,所以对于所有文件类型都因该是支持的。

新手刚学java,不足之处还请指正

2016年4月1日下午,《java入门123》翻开了第一页,从此走上不归路。新手初来乍到,献上见面礼的更多相关文章

  1. 2016年12月31日 学习java 第一天

    6个月没写代码了 现在从头开是学 又遇到了很基础的问题 以前配环境变量的时候  配过classpath  其实不要配classpath  因为运行的时候会优先去classpath去找 class文件  ...

  2. 摩根斯坦利 - 2016年09月8日 面试题 - HashMap

    摩根斯坦利 - 2016年09月8日 面试题: 给定一个 Map<Person, Object> map = new HashMap<Person, Object>(); 放入 ...

  3. 2016年3月15日Android实习日记

    1.解决了ScrollView滑动冲突问题. 2.设置好了“查看详解”与“题目编号”的部分. 3.完成了app启动图片的设置,并在启动的过程中开辟新的线程连接服务器并开启监听数据. 别忘了注册启动Ac ...

  4. 2016年12月31日 星期六 --出埃及记 Exodus 21:26

    2016年12月31日 星期六 --出埃及记 Exodus 21:26 "If a man hits a manservant or maidservant in the eye and d ...

  5. 2016年12月30日 星期五 --出埃及记 Exodus 21:25

    2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...

  6. 2016年12月29日 星期四 --出埃及记 Exodus 21:24

    2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...

  7. 2016年12月28日 星期三 --出埃及记 Exodus 21:23

    2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...

  8. 2016年12月27日 星期二 --出埃及记 Exodus 21:22

    2016年12月27日 星期二 --出埃及记 Exodus 21:22 "If men who are fighting hit a pregnant woman and she gives ...

  9. 2016年2月16日开始,每天一篇,记录学习心得,【基本技能篇】>>开篇《如何阅读一本书——心得》

    如何阅读一本书——心得 ——2016年2月12日 要达到阅读的所有目的,就必须在阅读不同书籍的时候,运用适当的不同速度.读的太快或太慢,都一无所获. 四个阅读层次:①基础阅读,具有基本阅读的能力,包括 ...

随机推荐

  1. MongoDB介绍及安装

    一.介绍: 1.NoSql(非关系型的数据库)成了一个极其热门的新领域,非关系数据库产品的发展非常迅速.MongoDB是NoSql的其中一种较为热门的非关系型数据库.查阅很多资料.其他博客和网站,借着 ...

  2. JS原型探索小记(一)

    最近,我学习了jquery的源码,有个很深的认识就是——当对js的基本语法和面向对象思维了解比较熟悉之后,js真正的精髓在通过阅读一些优秀的框架源码也显现出来,我个人总结为对原型(原型链)和闭包两个基 ...

  3. [转]linux 查看系统信息命令

    linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...

  4. 您的服务器没有安装这个PHP扩展:OpenSSL(其他平台API同步需要)

    今天在安装一个博客系统的时候提示这个错,在网上找了半天,自己慢慢弄出来的,具体如下: 1.找到你的php.ini 文件,将“;extension=php_openssl.dll”前面分号去掉. 2.复 ...

  5. java 中的2个接口 Comparable和Comparator

    像Integer.String这些类型的数据都是已经实现Comparable接口的,所以对这些类型可以直接通过Arrays.sort(...)和Collections.sort(...)方法进行排序. ...

  6. Hash Table 的实现步骤是什么

    什么是HashTable Hash Table 是计算机科学中很重要的一种数据结构,其时间复杂度为O(1),主要是通过把关键字Key 映射到数组中的一个位置来访问记录,所以速度相当快.映射函数称为 H ...

  7. VirtualMachine所支持的操作

    在JDK中com.sun.tools.attach.VirtualMachine提供了一些从外部进程attach到jvm上,并执行一些操作的功能.VirtualMachine的子类HotSpotVir ...

  8. 整形输出netsh的内容

    $raw = netsh wlan show network mode=bssid $ssids = $raw | Select-String -Pattern 'SSID\b'| Select-St ...

  9. Windows下获取本机IP地址方法介绍

    Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...

  10. java notify和notifyAll的区别

    首先从名字可以了解,notify是通知一个线程获取锁,notifyAll是通知所有相关的线程去竞争锁. notify不能保证获得锁的线程,真正需要锁,并且可能产生死锁. 举例1: 所有人(消费者线程) ...