package com.espeed.tool; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; /** * 用于创建文件夹的方法 * @param mkdirName */ public class createAndDeleteFile { /**创建文件夹*/ public static void mkdir(String mkdirName)throws Exception{ File dirFile = new File(mkdirName); boolean bFile = dirFile.exists(); if( bFile == true ) { System.out.println("The folder exists."); } else { System.out.println("The folder do not exist,now trying to create a one..."); bFile = dirFile.mkdirs(); if( bFile == true ) { System.out.println("Create successfully!"); System.out.println("创建文件夹"); } else{ System.out.println("Disable to make the folder,please check the disk is full or not."); System.out.println(" 文件夹创建失败,清确认磁盘没有写保护并且空件足够"); } } } /**删除文件夹*/ public static void deleteFile(File file){ try { if(file.exists()){ if(file.isFile()){ file.delete(); }else if(file.isDirectory()){ File files[] = file.listFiles(); for(int i=0;i<files.length;i++){ deleteFile(files[i]); } } file.delete(); }else{ System.out.println("所删除的文件不存在!"+'\n'); } } catch (Exception e) { e.printStackTrace(); } } /**用于创建文件的方法 */ public static void createFile(String mkdirName) throws Exception{ File dirFile = new File(mkdirName); boolean bFile = dirFile.exists(); if( bFile == true ){ System.out.println("The folder exists."); }else{ System.out.println("The folder do not exist,now trying to create a one..."); bFile = dirFile.createNewFile(); if( bFile == true ){ System.out.println("Create successfully!"); System.out.println("创建文件夹"); }else{ System.out.println("Disable to make the folder,please check the disk is full or not."); System.out.println(" 文件夹创建失败,清确认磁盘没有写保护并且空件足够"); } } } /**文件复制*/ public static void filecopy(File s, File t){ FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();//得到对应的文件通道 out = fo.getChannel();//得到对应的文件通道 in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] agrs){ createAndDeleteFile caf = new createAndDeleteFile(); File file = new File("J:mail\\xieyong435298846@qq.comdraft7320131122195807.eml"); caf.deleteFile(file); //mkdir("c:/aa/aa/aa"); } }