文件目录

时间:2016-11-07 00:56:50

标签: java file directory runtime execution

我正在编写一个程序,可以使用Java运行外部文件。我已经查找了如何运行.bat文件,但没有一个解决方案正在运行。似乎问题是我试图运行的文件的一个目录名中有一个空格。当我运行该程序时,会出现一个Windows错误,内容为

Windows cannot find 'C:\Users\Brandon's'. Make sure you typed the name correctly, and then try again

对于refrence,实际的用户文件名是Brandon的Pc,如果有空格,它似乎没有正确读取文件目录。我该怎么办?我无法在线找到解决方案。谢谢你的帮助!

Java中的参考程序代码

public static void main(String[] args) throws IOException {

     String currentDirectory = System.getProperty("user.dir");

//System.getProperty("user.dir") gets the directory of the java file

     File file = new File(currentDirectory+"\\src\\Start.bat");
     FileWriter writer = new FileWriter(file);
     PrintWriter outFile = new PrintWriter(writer);

//Writing information to a .bat file

     outFile.println("cd "+currentDirectory+"\\src\\CC\\CD\\");
     outFile.println("start CD.exe -o \"LC.txt\"");
     outFile.println("cd "+currentDirectory+"\\src\\ACr\\");
     outFile.println("BD.exe -f \"LAB.txt\"");
     outFile.println("exit");

//closes PrintWriter

     outFile.close();

//Excuting the /bat file

    Runtime.getRuntime().exec("cmd /c start "+file);
 }

0 个答案:

没有答案