在 Windows 和 Linux 机器上使用 Java 程序创建一个新文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4427270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a New folder using Java Program on Windows and Linux machines
提问by User 1034
How can I create a Folder using Java code on both Windows and Linux machines?
如何在 Windows 和 Linux 机器上使用 Java 代码创建文件夹?
采纳答案by Bozho
new File("/path/to/folder").mkdir();
If you want to created nested folders (i.e. more than one folder on the path may be missing), then use mkdirs(). See java.io.File.
如果要创建嵌套文件夹(即路径上可能缺少多个文件夹),请使用mkdirs(). 见java.io.File。
Note that forward slashes would normally not work on windows, but Java normalizes the path and translates forward to backward slashes.
请注意,正斜杠通常不适用于 Windows,但 Java 会规范化路径并将正斜杠转换为反斜杠。
回答by Jigar Joshi
try{
String strDirectoy ="test";
String strManyDirectories="dir1"+File.Separator+"dir2"+File.Separator+"dir3";
// Create one directory
boolean success = (new File(strDirectoy)).mkdir();
if (success) {
System.out.println("Directory: " + strDirectoy + " created");
}
// Create multiple directories
success = (new File(strManyDirectories)).mkdirs();
if (success) {
System.out.println("Directories: " + strManyDirectories + " created");
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
回答by thoredge
Use File.mkdir() (http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdir())
使用 File.mkdir() (http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdir())
回答by xromeo
try{
int a,b;
a=mk.dir();
b=newfile("\n new.java());
}

