'Decrease the number of calls

I have arraylist of filenames (java) and I want to delete these files using rm

  for(String check: checks){ 
here I am removing each file using rm 
 }

but it is time consuming can I do batching using xargs or something else which can help to delete files faster.



Solution 1:[1]

Don’t use rm. Use Java.

As others have pointed out, spawning a process is much slower than doing it in your program. Also, it’s just bad design to use a system command for something that can be done in code.

And finally, by using Files.delete, you gain platform independence. Your code is now write once, run anywhere!

Here’s what your loop would look like:

for (String check : checks) {
    Files.delete(Path.of(check));
}

This is essentially the same as what rm would do anyway.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VGR