Topic: 新手求教,拜托了(问题已解决!)(delete folder)

  Print this page

1.新手求教,拜托了(问题已解决!)(delete folder) Copy to clipboard
Posted by: kudo
Posted on: 2004-08-13 17:11

我用这个方法实现一个文件夹的删除(文件夹不一定是空的)
void delAll(String a)
{
  File m=new File( a );
  String str[]=m.list();
  if (str.length==0)
  {
    m.delete();
  }
  else
  {
    for (int i=0;i<str.length;i++ )
    {
      File n=new File(str[i]);
      if (n.isFile())
      {           n.delete();
      }
      else if (n.isDirectory())
      {
        delAll(n.getAbsolutePath());
      }
    }
  }
}

2.Re:新手求教,拜托了(帮我看看哪错了?) [Re: kudo] Copy to clipboard
Posted by: joelwx
Posted on: 2004-08-13 18:12

把你的错误信息贴出来啊!

3.Re:新手求教,拜托了(帮我看看哪错了?) [Re: kudo] Copy to clipboard
Posted by: kudo
Posted on: 2004-08-13 18:27

这个没有错误信息~因为没有语法的错误~
但实现不了预期的目标~
我想实现删除有内容的文件夹的功能,可是如果文件夹是空的可以删,但如果内有文件就删不了了~
我想通过递归实现,可是事与愿违~
拜托了,各位~

4.Re:新手求教,拜托了(帮我看看哪错了?) (delete folder) [Re: kudo] Copy to clipboard
Posted by: why
Posted on: 2004-08-13 22:28

Are you using *nix? Are there symbolic links?

I don't know what's wrong, but are you sure that list() returns the absolute paths with
String str[]=m.list();

You may want ot check what str[i] is (poor variable name) and whether
n.delete(); deletes the file.

BTW, I would use listFiles() instead.

5.Re:新手求教,拜托了(帮我看看哪错了?) (delete folder) [Re: kudo] Copy to clipboard
Posted by: kudo
Posted on: 2004-08-13 22:38

you add the symbol (delete folder) to my title?
thank you~I am a newcomer.not so familiar with the function of each class or package~and I am dying for your instructions~
by the way ,I will test your way~thanks~

6.Re:新手求教,拜托了(帮我看看哪错了?) (delete folder) [Re: kudo] Copy to clipboard
Posted by: why
Posted on: 2004-08-13 22:51

Yes, I always try to help to 尽量用准确的文字描述作为标题

I believe you can find many delete folder solution by searching on Google,
let me try keywords: Java delete folder
http://www.javalobby.org/kb/entry.jspa?entryID=193&categoryID=12
this one uses listFiles()
you will get more with "delete directory"

http://javaalmanac.com/ is sometimes helpful
e30. Deleting a Directory http://javaalmanac.com/egs/java.io/DeleteDir.html
this one uses list() -- from this example, I believe the problem with your code: File n=new File(str[i]);
str[i] is not an absolute path, try somthing like new File(dir, str[i]));

7.Re:新手求教,拜托了(帮我看看哪错了?) (delete folder) [Re: kudo] Copy to clipboard
Posted by: kudo
Posted on: 2004-08-14 00:30

you are so kind-hearted~my great honor~
I'll try,but it'll be really a tough work to read the pure Eng Doc~
do you always show up every evening?
YOU ARE GREAT!!

8.Re:新手求教,拜托了(帮我看看哪错了?) (delete folder) [Re: kudo] Copy to clipboard
Posted by: why
Posted on: 2004-08-14 01:10

There are a lot of good guys in this forum who are willing to help
as long as one has done his/her work (I mean researching on the problem) and phrases a question clearly.

kudo, please don't forget to show us your solution when you're done.
This is a way to help others as well.Smile

9.Re:新手求教,拜托了(问题已解决!) (delete folder) [Re: kudo] Copy to clipboard
Posted by: kudo
Posted on: 2004-08-15 13:51

经过多次的调试,终于实现最该方法~
感谢MR.WHY的指点,现在把正确方法贴出:
(代码可能有些冗余,有待改进,但完全可以实现删除非空文件夹)
  void delAll(String a)
  {
    File m=new File( a );
    String str[]=m.list();
    if (str==null)
    {
      m.delete();
    }
    else
    {
      for (int i=0;i<str.length;i++ )
      {
        File n=new File(m.getAbsolutePath(),str[i]);
        if (n.isFile())
        {
          n.delete();
        }
        else if (n.isDirectory())
        {
          delAll(n.getAbsolutePath());
          n.delete();
        }
      }
    }
    m.delete();
  }

除此方法这外,MR.WHY在上面推荐的从GOOGLE上搜出方法也非常简洁


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923