Java生成檔案夾
1、說明
判斷檔案夾是否存在,如果不存在就建立該檔案夾,并列印其路徑;如果存在,列印其路徑
2、實作源碼
package com.you.freemarker.model;
import java.io.File;
public class BuildFolder
{
public static String buildFolder(String path)
{
//讀取檔案夾路徑
File file = new File(path);
//判斷是否存在
if (!file.exists() && !file.isDirectory())
{
try
{
System.out.println("檔案夾不存在!");
//生成檔案夾
file.mkdir();
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
System.out.println("檔案夾存在!");
}
return path;
}
public static void main(String[] args)
{
String path = "D:\\Dong";
String folder = buildFolder(path);
System.out.println("檔案夾路徑:" + folder);
}
}
3、實作結果
(1)檔案夾不存在時
檔案夾不存在!
檔案夾路徑:D:\Dong

(2)檔案夾存在
檔案夾存在!
檔案夾路徑:D:\Dong
原文:http://blog.csdn.net/you23hai45/article/details/36058323