简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM