[英]Create a New folder using Java Program on Windows and Linux machines
如何在Windows和Linux计算机上使用Java代码创建文件夹?
new File("/path/to/folder").mkdir();
If you want to created nested folders (ie more than one folder on the path may be missing), then use mkdirs()
. 如果要创建嵌套文件夹(即路径上可能缺少多个文件夹),请使用
mkdirs()
。 See java.io.File
. 请参阅
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规范化路径并向前转换为反斜杠。
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());
}
使用File.mkdir()(http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdir())
try{
int a,b;
a=mk.dir();
b=newfile("\n new.java());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.