How to use mkdir and rmdir commands in a java prog

2019-02-13 15:06发布

I want to use system commands like mkdir and rmdir while running a java program.

How can I do that?

7条回答
等我变得足够好
2楼-- · 2019-02-13 15:54

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:

Runtime.getRuntime().exec("some_command");
查看更多
女痞
3楼-- · 2019-02-13 15:56

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("/d/temp/bin/mybatfile");

查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-13 15:57

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.

In your case you are looking for the java.io.File class, and specifically the mkdir and delete methods.

查看更多
Explosion°爆炸
5楼-- · 2019-02-13 16:02

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

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

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 envp into the exec(..) method call, which is basically the PATH variable from the environment.

查看更多
混吃等死
6楼-- · 2019-02-13 16:08

makedirectory method -

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

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

查看更多
三岁会撩人
7楼-- · 2019-02-13 16:12

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.

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.

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.

查看更多
登录 后发表回答