有没有一种方法可以一次创建一个文件和目录,如下所示...(使用Java 7和NIO ...“路径和文件”静态方法)。
在什么地方您不必键入Path,然后在单独的行(文件的代码)中归档?
File file = new File("Library\\test.txt");
if (file.getParentFile().mkdir()) {
file.createNewFile();
} else {
throw new IOException("Failed to create directory " + file.getParent());
}
基本上从Java 7 NIO中输入的Path(和file)中寻找等效的方法“ getParentFile()。mkdir()”。
谢谢
真正意识到它是这种方式的实现。
Path file = Paths.get("/Users/jokrasa/Documents/workspace_traffic/javaReviewFeb28/src/TEST/","testy.txt");
try {
Files.createDirectory(file.getParent());
Files.createFile(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
因此,您实际上不必键入两次...
干杯!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句