文件夹小记

import java.io.File;
import java.io.IOException;

public class dirtest {
    public static void main(String[] args) {
        //使用file指向一个文件
        File file=new File("E:\\新建文件夹\\readme.txt");

        if(file.exists()){
            file.delete();
        }else{
            //判断文件夹是否存在,因为文件夹不存在就创建不了文件
            File dir=file.getParentFile();
            if(!dir.exists()){
                dir.mkdirs();
            }
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/vincentmax/p/14240287.html