JavaSE_io_根据路径逐层创建文件夹 (代码实现)

来源:互联网 发布:mac怎么打开png格式 编辑:程序博客网 时间:2024/06/03 22:41


Java 中,创建 file 时,必须要 路径上的目录存在时,才能创建文件,否则会抛出异常。

所以需要对文件路径上的目录一一创建,下面给出这样一个实现。 



import java.io.File;/** * Created by szh on 2017/10/12. */public class DirectoryUtil {    private static String WIN_SEPARATOR = new String("\\");    private 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);        }    }    //Windows    public void createParentDirWIN(String path) throws Exception {        //Split中特殊字符分割: http://blog.csdn.net/myfmyfmyfmyf/article/details/37592711        // \ 用 “\\\\”        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());            }        }    }    //Linux    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\\");        }catch (Exception e){            e.printStackTrace();        }    }}


参考文章