package cn.net.cobot.cobot_biddata_servicemodel;
import java.io.File;
public class DirectoryUtil {
public static String WIN_SEPARATOR = new String("\\");
public static String LINUX_SEPARATOR = new String("/");
public void createParentDir(String path) throws Exception {
String systemSeparator = File.separator;
if (systemSeparator.equals(WIN_SEPARATOR)) {
createParentDirWIN(path);
} else if (systemSeparator.equals(LINUX_SEPARATOR)) {
createParentDirLinux(path);
}
}
public void createParentDirWIN(String path) throws Exception {
String[] pathArr = path.split("\\\\");
System.out.println("length : " + pathArr.length);
StringBuffer tmpPath = new StringBuffer();
for (int i = 0; i < pathArr.length; i++) {
tmpPath.append(pathArr[i]).append(WIN_SEPARATOR);
if (0 == i) continue;
File file = new File(tmpPath.toString());
if (!file.exists()) {
file.mkdir();
System.out.println("当前创建的目录是 : " + tmpPath.toString());
}
}
}
public void createParentDirLinux(String path) throws Exception {
String[] pathArr = path.split(LINUX_SEPARATOR);
StringBuffer tmpPath = new StringBuffer();
for (int i = 0; i < pathArr.length; i++) {
tmpPath.append(pathArr[i]).append(LINUX_SEPARATOR);
File file = new File(tmpPath.toString());
if (!file.exists()) {
file.mkdir();
System.out.println("当前创建的目录是 : " + tmpPath.toString());
}
}
}
public static void main(String[] args){
DirectoryUtil directoryUtil = new DirectoryUtil();
try{
directoryUtil.createParentDir("E:\\testCreate\\dmp\\FirstPartyAudience\\2017\\ss\\aa\\");
}catch (Exception e){
e.printStackTrace();
}
}
}