您的位置:首页 > 编程语言 > Java开发

疯狂Java讲义习题11.3

2017-08-06 18:16 218 查看
题目描述:

实现一个工具类,该工具可实现copy功能,若被copy的对象是文件,程序将制定文件复制到制定目录下,如过被copy对象是目录,程序应将该目录及其目录下的所有文件复制到指令目录下。 

代码如下:

点击(此处)折叠或打开

import java.io.*;

public class cpft

{

    public static void copyFile(String dst,String src) throws IOException

    {

        FileOutputStream fos = null;

        FileInputStream fis = null;    

            fos = new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));

            fis = new FileInputStream(src);

            int hasRead = 0;

            byte[] bbuf = new byte[1024];

            while ((hasRead = fis.read(bbuf)) > 0)

            {

                

                fos.write(bbuf,0,hasRead);

            }

        

    }

    public static void copy(String dst,String src) throws IOException

    {

        File newFile = new File(src);

        File[] fileList = newFile.listFiles();

        //File dstFile = new File(dst+"//"+newFile.getName());

        if (newFile.isFile())

        {

            

            copyFile(dst,src);

        }

        else

        {

            File dstFile = new File(dst+"//"+newFile.getName());

            if (!dstFile.exists())

            {

                dstFile.mkdirs();

            }

            for (File file:fileList)

            {

                if (file.isFile())

                        copyFile(dstFile.getAbsolutePath(),file.getAbsolutePath());

                else

                        copy(dstFile.getAbsolutePath(),file.getAbsolutePath());

            }            

        }

    }

    public static void main(String[] args) throws IOException

    {

        

        String src = "F:\\paper";

        String dst = "F:\\冰点文库";

        copy(dst,src);

        System.out.println("finished Copy");

    }

}

运行结果:(完成复制)

finished Copy


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

阅读(65) | 评论(0) | 转发(0) |

0
上一篇:疯狂Java讲义习题11.2

下一篇:疯狂java讲义习题11.4

相关热门文章
Tomcat 6 配置SSI

让Resin支持shtml(SSI)- 静...

tomcat + ssi

ASP JavaScript Lessons(8-14)

JDK1.6官方下载_JDK6官方下载_...

给主人留下些什么吧!~~

评论热议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: