Tran Anh Minh
Tran Anh Minh

Reputation: 306

Can Java delete all files and folders of a computer?

I'm just curious (but not willing to try on my computer) what would happen if I run this code on Java?

 private static void deleteAll(File file)
    {
        for(File f : file.listFiles())
        {
            if(f.isFile())
            {
                f.delete();
            }
            else
            {
                deleteAll(f);
                f.delete();
            }
        }
    }

    public static void main(String[] args) {

        File file = new File("/home");      
        deleteAll(file);
    }

Is this equivalent to rm -rf / in linux and will delete everything on my computer? Or will the operating system stop me from deleting System files?

Thank you so much!

Upvotes: 1

Views: 284

Answers (1)

mnestorov
mnestorov

Reputation: 4484

Okay this got my interest and I tried it on a lxc container. Now it wasn't a scientific test, but better than nothing right.

Well, I would say that I got mixed results. If you try to delete files with java normally, not system ones, then you are gonna do your work. Once you start killing your system, then thnings change. Java won't allow you to delete anything from your root directory, but I did successfuly delete my user folder (/home/ubuntusuer). I didn't corrupt the system, alas.

There is a mechanism to stop you from deleting a whole system. So the behaviour will not be exactly the same as compared to rm -rf /, but you can still remove a lot of stuff with it.

Upvotes: 1

Related Questions