如何在java程序中使用mkdir和rmdir命令 [英] How to use mkdir and rmdir commands in a java program
问题描述
我想在运行Java程序时使用 mkdir
和 rmdir
的系统命令。
I want to use system commands like mkdir
and rmdir
while running a java program.
我该如何做?
推荐答案
为什么要使用命令行? FYI,有内置的平台无关 File
类。
Why do you want to use the command line? FYI, there are built-in platform-independent File
classes.
http://www.exampledepot.com/egs/java.io/deletefile.html
http://www.roseindia.net/java/ beginners / java-create-directory.shtml
创建目录:
new File("dir path").mkdir();
删除目录:
new File("dir path").delete();
'new File'这里有一点不妥,不是真正创建目录一份文件。它创建一个Java资源钩子,您可以使用它来查询或操作现有的文件系统资源,或者根据您的请求创建一个新的资源钩子。否则,使用 Runtime.getRuntime()。exec(command line here)
使用命令行操作(不建议!!)。
'new File' here is a bit of a misnomer, it isn't actually creating the directory or a file. It's creating a Java resource hook which you can use to query or operate upon an existing filesystem resource, or create a new one at your request. Otherwise, use Runtime.getRuntime().exec("command line here")
for using command line operations (not advised!!).
编辑:整理出问题海报的问题:
sorted out the problem the question poster was having:
String envp[] = new String[1];
envp[0] = "PATH=" + System.getProperty("java.library.path");
Runtime.getRuntime().exec("command line here", envp);
注意 envp
code> exec(..)方法调用,这基本上是来自环境的 PATH
变量。
Note the insertion of envp
into the exec(..)
method call, which is basically the PATH
variable from the environment.
这篇关于如何在java程序中使用mkdir和rmdir命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!