Forum Stats

  • 3,769,824 Users
  • 2,253,026 Discussions
  • 7,875,217 Comments

Discussions

how to delete a directory

843798
843798 Member Posts: 24,864
Hi all,
I am trying to delete a directory using my java code as,

import java.io.*;

class dirinfo {
public static void main(String s[]){
deldir(s[0]);
}

public static void deldir(String st){
File f= new File(st);
if (f.isFile())
{
try{
Runtime.getRuntime().exec("del " + f.getAbsolutePath() );
return;
} catch(IOException e) {System.out.println("EXCEPTION: " + e);}
}

String[] fl= f.list();
for (int i=0;i<fl.length;i++)
deldir(fl);
}
}

It compiles without error. On giving a command like,
java dirinfo c:\test or c:\\test,
my test directory in c: doesnt have dirinfo.java/class file.
i get the following error:-

EXCEPTION : java.io.IOException CreateProcess del
d:\html\fileinfo.java error=0
Exception in thread "main"
java.lang.NullPointerException
at dirinfo.deldir(dirinfo.java:19)
at dirinfo.deldir(dirinfo.java:20)
at dirinfo.main(dirinfo.java:5)

Can anyone help???

thanx,
john.

Comments

  • 843798
    843798 Member Posts: 24,864
    String[] fl= f.list();
    for (int i=0;i<fl.length;i++)
    if fl is null, then doing fl.length will indeed give you a:
    java.lang.NullPointerException
    at dirinfo.deldir(dirinfo.java:19)
    at dirinfo.deldir(dirinfo.java:20)
    at dirinfo.main(dirinfo.java:5)
    Instead, try adding this:
    String[] fl= f.list();
    if (fl!=null){
        for (int i=0;i<fl.length;i++)
            deldir(fl);
    }
  • 843798
    843798 Member Posts: 24,864
    thanx a lot. yes u were right. i checked for null now its fine. but still i have a problem with Runtime.getRuntime.exec('c:\\windows\\command.com/c del " + f.getPath() + "\\" + f);

    what am trying to do here is to delete the file by getting its path. am working on windows ME. do u have any idea? the error am getting is,
    Exception java.io.IOException CreateProcess : filename.java\filename.java error=0

    here filename is 'fileinfo.java' as in my test directory.
  • 843798
    843798 Member Posts: 24,864
    Unless you have a specific reason not to do so, you'd probably be better off using the methods in the java.io.File class - it has its own delete() method which you can use on files and/or directories, and it is platform independent. Take a look at the API javadoc
  • 843798
    843798 Member Posts: 24,864
    under dos ( windows ) you can not "del" a directory.
    you need to use "rmdir"
    kurt
  • 843798
    843798 Member Posts: 24,864
    Hi all,
    I got the solution for deleting of directory. the code is :

    import java.util.zip.*;
    import java.util.jar.*;
    import java.io.*;

    class dirinfo
    {
    public static void main(String s[])
    {
    deldir(s[0]);
    System.out.println("DELETION COMPLETE.....!");
    }

    public static void deldir(String st)
    {
    File f= new File(st);


    if (f.isFile())
    {
    f.delete();
    return;
    }


    String[] fl=f.list();
    if (fl!=null && fl.length >0)
    {
    for (int i=0;i<fl.length;i++)
    {

    File f1= new File(f.getPath() + "\\" + fl);
    if(!f1.isFile())
    {
    deldir(f1.getPath());
    }
    else
    {
    deldir(f.getPath() + "\\" + fl[i]);
    }
    }

    System.out.println("Deleting empty dir 1
    ..."+ f.getPath());
    File f1=new File(f.getPath()); f1.delete(); return;


    } //-------if
    File f1=new File(f.getPath()); f1.delete(); return;
    }


    }


    THANX FOR ALL THE HELP
    :-)
  • 843798
    843798 Member Posts: 24,864
    I like to know the answer too.

    regards
This discussion has been closed.