文章对你有用的话免费点个赞哈,废话不多说,直接上代码:
public static void main(String[] args) {
String directorPath = "C:\\Users\\pc\\Desktop\\代码新建的文件夹哦";//拿我的代码去用觉得修改一下这一行哈,"代码新建的文件夹哦"改为“新建文件夹”
//创建文件夹
File directory = new File(directorPath);
if (!directory.exists()){
boolean created = directory.mkdir();
if (created){
System.out.println("文件夹已创建:"+directorPath);
}else {
System.out.println("无法创建文件夹:"+directorPath);
}
}else {
System.out.println("文件夹已经存在"+directorPath);
//尝试创建带编号的文件夹
int count = 1;
while (true){
String newDirectoryPath = directorPath + "("+count +")";
File newDirectory = new File(newDirectoryPath);
if (!newDirectory.exists()){
boolean created = newDirectory.mkdir();
if (created){
System.out.println("文件已创建"+newDirectoryPath);
break;
}else {
System.out.println("无法创建文件夹"+newDirectoryPath);
}
}
count++;
}
}
}