Skip to content
Advertisement

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement