在Windows环境下,如何在Java代码中通过cmd命令操作文件的增删改查?
比如 mkdir C:\test
     rd C:\test

解决方案 »

  1.   

    Java提供的Runtime有自己的环境,如果模拟的话建议用api模拟
      

  2.   


    能麻烦你给个完整的例子吗?
    我现在执行下面的代码:
    String[] command = new String[]{"md","C:\\test1"};
    try {
    Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    e.printStackTrace();
    }
    会抛出异常:
    java.io.IOException: Cannot run program "md": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:466)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 3 more
      

  3.   


    能麻烦你给个完整的例子吗?(在Windows环境下)
    我现在执行下面的代码:
    String[] command = new String[]{"md","C:\\test1"};
    try {
    Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    e.printStackTrace();
    }
    会抛出异常:
    java.io.IOException: Cannot run program "md": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:466)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 3 more
      

  4.   


    能麻烦你给个完整的例子吗?(在Windows环境下)
    我现在执行下面的代码:
    String[] command = new String[]{"md","C:\\test1"};
    try {
    Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    e.printStackTrace();
    }
    会抛出异常:
    java.io.IOException: Cannot run program "md": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:466)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 3 more
      

  5.   


    public static void main(String[] args) {
    try {
    Process p = Runtime.getRuntime().exec("cmd.exe /c md C:\\test1");
    while(p.getInputStream().read()!=-1);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }