1。如何删除D:\a下的所有文件?但要保留a文件加。他下面的文件都要删除?
2。还有,如何把d:\a下所有的文件复制到f:\b文件jia下面去呢?

解决方案 »

  1.   

    public static void delFile(File directory) throws Exception
    {
      if (!directory.exists())
        throw new IOException(directory.toString()+" do not exist!");
      String[] filelist = directory.list();
      File tmpFile = null;
      for (int i = 0; i < filelist.length; i++) {
        tmpFile = new File(directory.getAbsolutePath(),filelist[i]);
        if (!tmpFile.isDirectory()) {
          tmpFile.delete();
        }
      }
    }
    剪切相当于拷贝加删除,参考http://community.csdn.net/Expert/TopicView3.asp?id=4654001
      

  2.   

    C:\Documents and Settings\Administrator>del /?
    删除一个或数个文件。DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
    ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names  names         指定一个或数个文件或目录列表。通配符可被用来
                    删除多个文件。如果指定了一个目录,目录中的所
                    有文件都会被删除。  /P            删除每一个文件之前提示确认。
      /F            强制删除只读文件。
      /S            从所有子目录删除指定文件。
      /Q            安静模式。删除全局通配符时,不要求确认。
      /A            根据属性选择要删除的文件。
      attributes      R  只读文件                     S  系统文件
                      H  隐藏文件                     A  存档文件
                      -  表示“否”的前缀C:\Documents and Settings\Administrator>copy /?
    将一份或多份文件复制到另一个位置。COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
         [+ source [/A | /B] [+ ...]] [destination [/A | /B]]  source       指定要复制的文件。
      /A           表示一个 ASCII 文本文件。
      /B           表示一个二进位文件。
      /D           允许解密要创建的目标文件
      destination  为新文件指定目录和/或文件名。
      /V           验证新文件写入是否正确。
      /N           复制带有非 8dot3 名称的文件时,
                   尽可能使用短文件名。
      /Y           不使用确认是否要改写现有目标文件
                   的提示。
      /-Y          使用确认是否要改写现有目标文件
                   的提示。
      /Z           用可重新启动模式复制已联网的文件。
      

  3.   

    文件复制代码
    package filecopy;import java.io.*;public class Filecopy { /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    if (args.length == 0)
    args = new String[] { "d:\\a", "f:\\b" };
    //没有参数默认为"d:\\a", "f:\\b"
    File frompath = new File(args[0]);//
    File topath = new File(args[1]);
    String[] fromlist = frompath.list();
    for (int i = 0; i < fromlist.length; i++) {
    File fromfile = new File(frompath.getPath(), fromlist[i]);
    File tofile = new File(topath.getPath(), fromlist[i]);
    if (fromfile.isDirectory()) {
    tofile.mkdirs();
    System.out.println( fromfile.getCanonicalPath());
    main(new String[] { fromfile.getPath(), tofile.getPath() });
    }
    else {
    int ch;
    byte[] b = new byte[256];
    RandomAccessFile fin = new RandomAccessFile(fromfile, "r");
    RandomAccessFile fout = new RandomAccessFile(tofile, "rw");
    ch = fin.read(b);
    while (ch != -1) {
    fout.write(b);
    ch = fin.read(b);
    }
    fin.close();
    fout.close();
    }
    } }
    }
      

  4.   

    赞同yuzl32的办法,调用操作系统指令。
    几行脚本搞定的事,用java太繁琐了。
      

  5.   

    以下是删除代码:
    package filedel;import java.io.*;public class Filedel { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    if(args.length==0)
    args=new String[]{"g:\\1"};
    File filepath= new File(args[0]);
    String[] filelist = filepath.list();
    for (int i=0;i<filelist.length;i++){
    File f = new File(filepath.getPath(),filelist[i]);
    if (f.isDirectory())
    {
    main(new String[] {f.getPath()});
    }
    f.delete();
    }
    }}