0

在我当前的项目中,我正在编写代码生成器。界面将是命令行。

在命令行参数中,用户指定规范文件和目标文件夹,这将指导代码生成器在特定目标文件夹中生成文件。

例如,我的命令行参数是

VocArchSpec.text NetworkSpec.text ./src/fr/inria/arles/pankesh/gen ./src/fr/inria/arles/pankesh/gen/logic ./src/fr/inria/arles/pankesh/gen/sim/device ./src/fr/inria/arles/pankesh/gen/util

在上述命令中,1 和 2 是规范,其他是目标路径。此路径由代码生成器使用。代码生成器在指定的目标文件夹中生成几个 java 文件。

但是,我的问题是我收到以下错误:

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)

===============> 我使用以下代码生成文件。

new File(GlobalVariable.utilDirPath).mkdir(); // Assume that I set the value GlobalVariable.utilDirPath variable through command line arguments. 
file = new File(GlobalVariable.utilDirPath + "/" + unit.getName());
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(content);
writer.flush();
Writer.close();
4

1 回答 1

1

获得此异常的一种方法是当要创建文件的路径(文件夹)不存在时。您的代码需要确保指定的文件夹存在。

要创建这些的简单方法,请查看File.mkdirs()

于 2012-06-18T17:29:54.633 回答