Delete all file folder using java




















The bigger the company you work for, the more red tape you have to go through. A company that needs an army of lawyers to allow to use apache commons is the absolute pathology, and nothing near the norm in IT world. I've never heard of anyone having such problems and if you have such problems you have most likely access to SO blocked so the answer would not be accessible to you anyway.

Show 3 more comments. Cemron Cemron 1, 1 1 gold badge 11 11 silver badges 15 15 bronze badges. This won't delete non-empty subdirectories. Be VERY careful. If index is a symbolic link to another directory, you'll wind up deleting the contents of the other directory. Unfortunately, I've not yet found a good way to detect symbolic links on Windows in Java 6, though Java 7 provides Files. Solution: wrap this chunk of code in if! Then, if index is a symbolic link, it's deleted regardless of whether it appears it has contents.

The code should check whether entries is null. Add a comment. Jeff Learman Jeff Learman 2, 1 1 gold badge 19 19 silver badges 28 28 bronze badges.

As it turns out, there is a bug in this. If another process deletes the files during the loop, it can cause an exception that should be caught and ignored. The OP said nothing about symbolic links in his post. Just creating and deleting a directory. Please also list out the "many things missing". Help us out. PerryTew I'm not being snarky. I'm just pointing out, that I do fully disagree with your comment about the answer being better because no external libraries are being used.

It is not. There is a good reason people are using apache commons : you don't have to program any single thing yourself. Symlinks are just an example of things you'll miss writing everything from scratch. Not relying on external libraries is sometimes a significant benefit. Of course, there's a significant benefit to using tried-and-true software.

It's up to the developer to balance the issues. If there are bugs other than the two already mentioned, we'd certainly like to know about them. I prefer this solution on java 8: Files. Note that this may have scalability issues since it builds the full list, creates a sorted copy, and then iterates over the sorted copy.

Back in the bad old days when memory wasn't inexhaustible, this would be a very bad idea. This wouldn't matter in most use cases. I should have said "space O N vs O depth " above, where depth is the depth of the directory tree comparing this solution to recursive ones. Doesn't this have the problem of file handle leaks? This example does not close the stream returned by Files.

I know that if you don't close the stream returned by Files. See e. You don't call any terminal function in that stream, for this reason no files will actually be deleted. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking.

Computer Graphics. But with Java 7, we can do this using Files class. Below is the code that you should use to delete a directory. It takes care of deleting nested directories too. Your email address will not be published.

Prev Java create new file. Like Article. Syntax: public boolean delete Returns: true if and only if the file or directory is successfully deleted; false otherwise. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Previous Copying file using FileStreams in Java.

Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.



0コメント

  • 1000 / 1000