简体   繁体   English

使用file.delete()删除远程文件更好,或者使用rm -rf进行ProcessBuilder调用更好?

[英]Deleting remote files is better with file.delete() or making ProcessBuilder call with rm -rf?

If file.delete() is better, how is it? 如果file.delete()更好,那怎么办? What are the problems of using ProcessBuilder? 使用ProcessBuilder有什么问题? Also I have to loop through a list to delete the different files. 另外,我还必须遍历列表以删除不同的文件。 In such a scenario, what is the best approach? 在这种情况下,最佳方法是什么?

Using java File.delete is better approach, since: 使用Java File.delete是更好的方法,因为:

i) Java code will be portable across different OS, if used File.delete() instead of executing OS dependent commands on ProcessBuilder Api. i)如果使用File.delete()而不是在ProcessBuilder Api上执行与OS相关的命令,则Java代码将可以在不同的OS上移植。 ii) A java programmer can easily understand the functionality written to delete list of different files. ii)Java程序员可以轻松理解为删除不同文件列表而编写的功能。

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 i)由于Java不允许删除一个非空目录,我们需要写一个实用程序的方法来递归删除目录中的文件,如图这里 ,不像使用的ProcessBuilder的API,它消除了与单个命令RM -rf目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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