我知道可以用FileOutputStream和FileintputStream来实现文件复制,但怎么复制整个文件夹(下面有子文件夹或者很多文件)呢?

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【samcy100】截止到2008-07-29 16:57:52的历史汇总数据(不包括此帖):
    发帖的总数量:9                        发帖的总分数:310                      每贴平均分数:34                       
    回帖的总数量:11                       得分贴总数量:1                        回帖的得分率:9%                       
    结贴的总数量:9                        结贴的总分数:310                      
    无满意结贴数:1                        无满意结贴分:30                       
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:11.11 %               无满意结分率:9.68  %                  
    敬礼!

    http://www.java2000.net/mycsdn/robotStop.jsp
      

  2.   

    /**
         * 拷贝一个目录或者文件到指定路径下
         * 
         * @param source
         * @param target
         */
        public static void copy(File source, File target)
        {
            File tarpath = new File(target, source.getName());
            if (source.isDirectory())
            {
                tarpath.mkdir();
                File[] dir = source.listFiles();
                for (int i = 0; i < dir.length; i++)
                {
                    copy(dir[i], tarpath);
                }
            }
            else
            {
                try
                {
                    InputStream is = new FileInputStream(source);
                    OutputStream os = new FileOutputStream(tarpath);
                    byte[] buf = new byte[1024];
                    int len = 0;
                    while ((len = is.read(buf)) != -1)
                    {
                        os.write(buf, 0, len);
                    }
                    is.close();
                    os.close();
                }
                catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }