使用 java.io.file 创建文件(文件夹),算是 java 最基础的知识,但实战项目中还是需要知晓细节。
比如 File 类中的 mkdir() 和 mkdirs() 的区别。
package supermarket;import java.io.File;import java.io.IOException;import java.nio.file.*;public class Filetest { public void filetest(){ File file1 = new File("D:/AAA/BBB/CCC"); if (!file1 .exists() && !file1 .isDirectory()) { System.out.println("//不存在"); file1 .mkdir(); if (file1.mkdirs()) { System.out.println("多级层文件夹创建成功!创建后的文件目录为:" + file1.getPath() + ",上级文件为:" + file1.getParent()); } File file2 = new File("D:/AAA/BBB/CCC/DDD"); if (file2.mkdir()) { System.out.println("单文件夹 DDD 创建成功!创建后的文件目录为:" + file2.getPath() + ",上级文件为:" + file2.getParent()); } File file3 = new File("D:/AAA/BBB/CCC/DDD","mytest.txt"); try { if (file3.createNewFile()) { System.out.println("多级层文件夹下文件创建成功!创建了的文件为:" + file3.getAbsolutePath() + ",上级文件为:" + file3.getParent()); } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("//目录存在"); File file3 = new File("D:/AAA/BBB/CCC/DDD","mytest.txt"); file3.delete(); System.out.println("删除成功"); String path = "D:/AAA/BBB/CCC"; File file = new File(path); String[] tempList = file1.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { //先删除文件夹里面的文件 delAllFile(path + "/" + tempList[i]); //再删除空文件夹 delFolder(path + "/" + tempList[i]); System.out.println("overoverover"); } } } } private void delFolder(String string) { // TODO Auto-generated method stub } private void delAllFile(String string) { // TODO Auto-generated method stub } } 上述代码是第一天拷贝了网上的。没有修改好。 删除文件怎么都删除不掉,经过今天的修改,终于可以实现循环删除了,暗自庆幸下。 代码如下:
package supermarket;import java.io.File;import java.io.IOException;import java.nio.file.*;public class Filetest { public boolean deletefile(String delpath) throws Exception { File file = new File(delpath); if (file.isDirectory()) { String[] filelist = file.list(); for (String delFile : filelist) { File delfile = new File(delpath + File.separator + delFile); if (delfile.isDirectory()) { deletefile(delpath + File.separator + delFile); } else System.out.println("正在删除文件:" + delfile.getPath() + ",删除是否成功:" + delfile.delete()); } System.out.println("正在删除空文件夹:" + file.getPath() + ",删除是否成功:" + file.delete()); } else System.out.println("正在删除文件:" + file.getPath() + ",删除是否成功:" + file.delete()); return true; } public void filetest() throws Exception{ File file1 = new File("D:/AAA/BBB/CCC"); if (!file1 .exists() && !file1 .isDirectory()) { System.out.println("//不存在"); file1 .mkdir(); if (file1.mkdirs()) { System.out.println("多级层文件夹创建成功!创建后的文件目录为:" + file1.getPath() + ",上级文件为:" + file1.getParent()); } File file2 = new File("D:/AAA/BBB/CCC/DDD"); if (file2.mkdir()) { System.out.println("单文件夹 DDD 创建成功!创建后的文件目录为:" + file2.getPath() + ",上级文件为:" + file2.getParent()); } File file3 = new File("D:/AAA/BBB/CCC/DDD","mytest.txt"); try { if (file3.createNewFile()) { System.out.println("多级层文件夹下文件创建成功!创建了的文件为:" + file3.getAbsolutePath() + ",上级文件为:" + file3.getParent()); } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("//目录存在"); Filetest file3 = new Filetest();// file3.delete(); file3.deletefile("D:/AAA"); System.out.println("删除成功"); } } }//Filetest ff = new Filetest(); try { ff.filetest(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
结果如下所示: