Java-0024-用I/O实现拷贝文件

2016.8.5

若是文件就拷贝,
若是文件夹就深层遍历拷贝。

    public static void copy(String fileName,String newFileName){

        if(fileName==newFileName){
            System.out.println("文件名不能重复");
            return ;
        }

        File file = new File(fileName);
        File newFile = new File(newFileName);

        if(!file.exists()){
            System.out.println("文件不存在,拷贝失败!");
        }
        else if(file.isFile()){
            copyFile(fileName,newFileName);
        }
        else if(file.isDirectory()){
            newFile.mkdirs();

            for(String name:file.list()){
                System.out.println(name);
                copy(fileName+"\\"+name,newFileName+"\\"+name);
            }

        }

    }

具体拷贝方法
不过这个只适合拷贝文本,拷贝图片之类的就不能成功拷贝了,文件大小会变,也无法正常打开
拷贝任意格式文件参见下一个方法

    public static void copyFile(String fileName,String newName){

        File file = new File(fileName);
        File newFile = new File(newName);
        
        if(!file.exists()){
            System.out.println("文件不存在,拷贝失败!");
        }
        else if(file.isDirectory()){
            System.out.println("这不是一个文件,拷贝失败!");
        }
        else{
            //只能拷贝文本,拷贝其他并不能成功,大小会变,内容也乱了
            BufferedReader br=null;
            BufferedWriter bw=null;

            try {
                if(!newFile.exists()){
                    newFile.createNewFile();
                }

                FileReader reader = new FileReader(file);
                FileWriter writer = new FileWriter(newFile);
                br = new BufferedReader(reader);
                bw = new BufferedWriter(writer);

                String line=br.readLine();
                while(line!=null){
                    bw.write(line);
                    bw.newLine();
                    line=br.readLine();
                    bw.flush();
                }
                
                System.out.println("拷贝成功!");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(br!=null){
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(bw!=null){
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }   
    }

这个方法可以拷贝任意格式的文件

    public static void copyFile(String fileName,String newName){

        File file = new File(fileName);
        File newFile = new File(newName);
        
        if(!file.exists()){
            System.out.println("文件不存在,拷贝失败!");
        }
        else if(file.isDirectory()){
            System.out.println("这不是一个文件,拷贝失败!");
        }
        else{
            //可以拷贝任意格式的文件
            FileInputStream input=null;
            FileOutputStream output=null;
            byte[] b=new byte[128];
            int length;

            try {
                if(!newFile.exists()){
                    newFile.createNewFile();
                }

                input = new FileInputStream(file);
                output = new FileOutputStream(newFile);

                while(true){
                    length=input.read(b);
                    if(length==-1){
                        break;
                    }
                    output.write(b,0,length);
                    output.flush();
                }

                System.out.println("拷贝成功!");        
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(input!=null){
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(output!=null){
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }   
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,458评论 30 472
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 100,198评论 9 468
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,222评论 19 139
  • 郎骑竹马来 绕床弄青梅 同居长千里 两小无嫌猜 记忆里自从小学毕业以后就彻底的和六一说了再见 在二十多年的青春岁月...
    何无敌阅读 3,068评论 0 0
  • 如果说此生有你不离我不弃的ta,那想必就非你莫属了————影子 铛铛铛……hello.are you ok?是的,...
    南城以南_菜菜阅读 4,155评论 5 5

友情链接更多精彩内容