public static void createFile() throws IOException{ String path0 = "E:/evan"; String path1 = "E:/evan/zl.txt"; //创建文件时,这个文件夹必须存在,否则会报错。也就是“E:/evan”这个文件夹必须存在 File file = new File(path0); //创建文件夹 if(!file.exists()){ file.mkdir(); } //创建文件 file = new File(path1); if(!file.exists()){ file.createNewFile(); } }
我用这个主要是图片,文件上传的时候,如果服务器不存在此文件夹,那么会自动创建。