Question

If file.delete() is better, how is it? What are the problems of using ProcessBuilder? Also I have to loop through a list to delete the different files. In such a scenario, what is the best approach?

Was it helpful?

Solution

Using java File.delete is better approach, since:

i) Java code will be portable across different OS, if used File.delete() instead of executing OS dependent commands on ProcessBuilder Api. ii) A java programmer can easily understand the functionality written to delete list of different files.

Cons:

i) Since java doesn't allow delete a non-empty directory, we need to write a util method to recursively delete files in directory as shown here, unlike using ProcessBuilder Api, which removes the directory with single command rm -rf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top