如何在 Java 程序中使用 mkdir 和 rmdir 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2693740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:54:30  来源:igfitidea点击:

How to use mkdir and rmdir commands in a java program

javacommand-linecmd

提问by Andersson Melo

I want to use system commands like mkdirand rmdirwhile running a java program.

我想用系统命令状mkdir,并rmdir在运行的Java程序。

How can I do that?

我怎样才能做到这一点?

采纳答案by Chris Dennett

Why do you want to use the command line? FYI, there are built-in platform-independent Fileclasses.

为什么要使用命令行?仅供参考,有内置的平台无关File类。

http://www.exampledepot.com/egs/java.io/deletefile.html
http://www.roseindia.net/java/beginners/java-create-directory.shtml

http://www.exampledepot.com/egs/java.io/deletefile.html
http://www.roseindia.net/java/beginners/java-create-directory.shtml

Make directory:

制作目录:

new File("dir path").mkdir();

Remove directory:

删除目录:

new File("dir path").delete(); 

'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!!).

这里的“新文件”有点用词不当,它实际上并不是在创建目录或文件。它正在创建一个 Java 资源挂钩,您可以使用它来查询或操作现有的文件系统资源,或者根据您的要求创建一个新的资源。否则,Runtime.getRuntime().exec("command line here")用于使用命令行操作(不建议!!)。

Edit: 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);

Note the insertion of envpinto the exec(..)method call, which is basically the PATHvariable from the environment.

注意插入envpexec(..)方法调用中,这基本上是PATH来自环境的变量。

回答by Yishai

The best is not to, but rather find the pure Java API function that does it. It is cleaner, easier to understand and much less error prone. It is also the only way to do Java that is write once run everywhere. Once you are calling shell commands, you are tied to that shell.

最好的方法不是,而是找到能够做到这一点的纯 Java API 函数。它更清晰、更容易理解并且更不容易出错。它也是实现一次编写到处运行的 Java 的唯一方法。一旦你调用 shell 命令,你就被绑定到那个 shell。

In your case you are looking for the java.io.Fileclass, and specifically the mkdirand deletemethods.

在您的情况下,您正在寻找java.io.File类,特别是mkdirdelete方法。

回答by revdrjrr

There are methods for this in the Java API, and nicely wrapped implementations in commons-io that simplify the process further.

Java API 中有用于此的方法,并且在 commons-io 中很好地包装了实现,以进一步简化过程。

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

回答by cyphorious

As the other mentioned, you shouldn't do this for simple file management. But to have it mentioned: The Java API has a class called Runtime, that allows system calls... for example:

正如另一个提到的,您不应该为了简单的文件管理而这样做。但要提到的是:Java API 有一个名为Runtime的类,它允许系统调用......例如:

Runtime.getRuntime().exec("some_command");

回答by Jules

For reference of people stumbling onto this question and wondering why Runtime.getRuntime().exec("mkdir foo")doesn't work even when incorporating the environment as per Chris Dennett's answer, the most probable reason is that you don't have a program called "mkdir" on your system. While most Unix-like systems have a program of this name, it isn't absolutely necessary for them to have one, and Windows doesn't have one, because in both cases the shell interprets this command itself, rather than passing it to an external program.

对于那些绊倒这个问题并想知道为什么Runtime.getRuntime().exec("mkdir foo")即使按照 Chris Dennett 的回答合并环境也不起作用的人,最可能的原因是您的系统上没有名为“mkdir”的程序。虽然大多数类 Unix 系统都有一个同名的程序,但它们并不是绝对有必要,而 Windows 没有,因为在这两种情况下,shell 都会解释这个命令本身,而不是将它传递给一个外部程序。

To make it work, try ...exec ("cmd /c mkdir foo")for NT-family Windows (or "command /c mkdir foo"for Windows 95 family), or exec ("sh -c \"mkdir foo\"")for Unix.

要使其工作,请尝试...exec ("cmd /c mkdir foo")用于 NT 系列 Windows(或"command /c mkdir foo"Windows 95 系列)或exec ("sh -c \"mkdir foo\"")Unix。

The fact that there isn't a platform-independent way to do this is yet another reason to prefer the Java APIs for performing the task.

没有独立于平台的方法来执行此操作的事实是更喜欢 Java API 来执行任务的另一个原因。

回答by roshi

Hi Agree to the fact of not been platform independent but just for testing an app I had to use it. The solution in my case for the Runtime.getRuntime().exec("my_command_name") ; for not working was i had to give the full path to where the batch/sh/executable file was ie:

嗨 同意不是独立于平台的事实,而只是为了测试我必须使用它的应用程序。在我的案例中 Runtime.getRuntime().exec("my_command_name") 的解决方案;因为不工作,我必须提供批处理/sh/可执行文件所在位置的完整路径,即:

Runtime.getRuntime().exec("/d/temp/bin/mybatfile");

Runtime.getRuntime().exec("/d/temp/bin/mybatfile");

回答by Shiva

makedirectory method -

makedirectory 方法 -

File dir = new File("path name");
boolean isCreated = dir.mkdir();

delete method -

删除方法——

public boolean delete()
true if and only if the file or directory is successfully deleted; false otherwise

For more refer below links -

有关更多信息,请参阅以下链接 -

https://docs.oracle.com/javase/8/docs/api/java/io/File.html

https://docs.oracle.com/javase/8/docs/api/java/io/File.html

http://www.flowerbrackets.com/create-directory-java-program/

http://www.flowerbrackets.com/create-directory-java-program/