A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 天子万年 中级黑马   /  2017-7-8 21:15  /  1243 人查看  /  5 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

package pps;
import  java.io. * ;
public   class  qw {
       
    public static void main(String[] args) {  

        fileCopy("C:\\123", "D:");  //调用方法并将源和汇以参数形式传递进函数。
    }  

     public static   void  fileCopy(String a, String b){//函数体声明
        File file  =   new  File(a);//将文件源封装成对象

         if ( ! file.exists()){//判断该文件源是否存在,如果不存在则提示并结束函数。
            System.out.println(a  +   "  Not Exists. " );
             return ;
        }
        File fileb  =   new  File(b);//将汇封装成对象。

         if (file.isFile()){//判断该对象是否是一个文件
            FileInputStream fis  =   null ;
            FileOutputStream fos  = null ;
             try  {
                fis  =   new  FileInputStream(file);//关联输入流
                fos  =    new  FileOutputStream(fileb);//关联输出流

                 byte [] bb  = new   byte [ ( int )file.length()];//创建缓冲区
                fis.read(bb);
                fos.write(bb);

            } catch  (IOException e){
                e.printStackTrace();
            } finally {
                 try  {
                    fis.close();
                    fos.close();
                }  catch  (IOException e) {
                    e.printStackTrace();
                }
            }
        } else   if (file.isDirectory()){//判断此对象是否是文件夹
             if ( ! fileb.exists()){//判断此对象的抽象路径名是否存在,如果不存在则新建
                fileb.mkdir();
            }
            String[] fileList;
            fileList  =  file.list();
             for ( int  i  =   0 ; i  <  fileList.length; i ++ ){
                fileCopy(a  +   " \\ "   +  fileList[i],b  +   " \\ "   +  fileList[i]);//递归调用
            }
        }
    }   
}


为啥总提示错误呢?如下:
C:\123 \ 12  Not Exists.
C:\123 \ data.txt  Not Exists.
C:\123 \ desktop.ini  Not Exists.
文件夹明明存在的啊?

5 个回复

倒序浏览
各位师兄别只看不说话好不
回复 使用道具 举报
把最后的\\换成/就好了
回复 使用道具 举报
fileCopy(a  +   " \\ "   +  fileList[i],b  +   " \\ "   +  fileList[i]);//递归调用
这一行中\\后面不要有空格,, fileCopy(a  +   "\\"   +  fileList[i],b  +   "\\"   +  fileList[i]);
回复 使用道具 举报
hongpiwa 发表于 2017-7-8 23:53
fileCopy(a  +   " \\ "   +  fileList,b  +   " \\ "   +  fileList);//递归调用
这一行中\\后面不要有空 ...

多谢可是为什么复制完之后没有123这个文件夹,只有他的子文件夹和文件
回复 使用道具 举报
顺着你的程序执行过程走一边,,你直接复制的就是123文件夹下面的子文件,123这个文件夹你并没有复制
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马