/**
     * Delete the files in the specified folder.
     * @param strPath
     */
    public static boolean delFileInFolder(String strPath)
    {
        File f = new File(strPath);
        if((f.exists()) && ( f.isDirectory()))
        {
            Vector v = new Vector();
            getFilesRecursive(f,v);
            for(int i=0;i<v.size();i++)
            {
                String strDelPath = (String)v.elementAt(i);
                System.out.println("Delete file:" + strDelPath);
                try
                {
                    File fDel = new File(strDelPath);
                    if( fDel.exists() && fDel.isFile() )
                        fDel.delete();
                }
                catch(Exception e)
                {
                    System.out.println("Can not delete file:" + strDelPath);
                    return false;
                }
            }
            return true;
        }
        else
        {
            System.out.println("Can not delete files in " + strPath);
            return false;
        }
    }

解决方案 »

  1.   

    /**
         * Get the all the filenames with absolute path
         * All the files fullpath will be store in vector
         * @return the path name
         */
        public static void getFilesRecursive(File dir,Vector v)
        {
            if(dir.isDirectory())
            {
                String path = dir.getAbsolutePath();
                String sub[] = dir.list();
                for(int i = 0;i<sub.length;i++)
                {
                    File f = new File(path + File.separator + sub[i]);
                    getFilesRecursive(f,v);
                }
            }
            else
            {            String tmp = dir.getAbsolutePath();
                v.add(tmp);
                //System.out.println(tmp);
            }    }
      

  2.   

    Revise the upper method
      

  3.   

    有一种办法是列出所有的文件,把它们rename到目的文件夹去,初次之外还有其它办法吗?有没有现成的拷贝方法?
      

  4.   

    我的方法错了,不能用rename不然原文件夹中的文件就不存在了。
      

  5.   

    dmhorse(dmhorse)
    你的方法可以列出所有的文件,但这不是问题的所在了,我想要得是一个copy的方法。
    难道我只有每个文件都打开和写入吗?
      

  6.   

    this.copyallfile("c:\\1","c:\\2");
    public  void  copyallfile(String  path1,String  path2)  throws  IOException
            {
              File f=new File(path1);
              if(f.isDirectory() ){
           File files[]=f.listFiles() ;
            for (int i = 0; i < files.length; i++) {
             FileInputStream fi = new FileInputStream(files[i].toString());
               FileOutputStream fo = new FileOutputStream(path2+"\\"+files[i].getName());
             byte date[] = new byte[fi.available()];
               fi.read(date);
               fo.write(date);
               fi.close();
                 fo.close();
           }
          
            } 俺测试过的!
      

  7.   

    ok了,谢谢,马上结帖,我稍稍改了一下。
    public static void copyallfile(String path1, String path2)
    throws IOException {
    File f = new File(path1);
    if (f.isDirectory()) {
    File files[] = f.listFiles();
    for (int i = 0; i < files.length; i++) {
    if (!files[i].isDirectory()) {
    FileInputStream fi =
    new FileInputStream(files[i].toString());
    FileOutputStream fo =
    new FileOutputStream(path2 + "\\" + files[i].getName());
    byte date[] = new byte[fi.available()];
    fi.read(date);
    fo.write(date);
    fi.close();
    fo.close();
    } else {
    String str = path2 + "\\" + files[i].getName();
    File diro = new File(str);
    if (!diro.exists()) {
    diro.mkdir();
    }
    copyallfile(files[i].getPath(), str);
    }
    } }
    }