简体   繁体   中英

How to Wait till all files are deleted by my java function?

I am having a problem.
I have a function to remove some files and then reboot the system. It contains a lot of files, so before the whole set of files are getting deleted, my reboot command is getting executed.

How can I solve this in my Java code.

To delete the files, I am using:

Runtime.exec("/bin/rm -rf /myDir");
Runtime.exec("/bin/shutdown -r now");

You can use the Process returned by exec and wait until it finishes:

Process p = Runtime.exec("/bin/rm -rf /myDir");
p.waitFor();
Runtime.exec("/bin/shutdown -r now");

您可以使用Process.waitFor()等待命令完成,也可以合而为一。

Runtime.exec("/bin/sh -c '/bin/rm -rf /myDir ; /bin/shutdown -r now'");

The Runtime.exec methods returns a Process on which you can call the waitFor method .

Do it for the first command and launch the second one just after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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