/**

  * 删除文件

  *

  * @param comp

  * @param filename  如:ctt.txt

  * @throws Exception

  */

 public static void deleteFile(CompanyInfo comp, String filename) {

  String path = Global.getProperty(Constant.system_company_site_dir)

      + comp.getDomain(); // 取得静态工程的路径并加上对应的公司2级域名

  String oldFileStr = path + filename; //得到文件的全路径

  File f = new File(oldFileStr); //新建一个File文件,路径塞进去

  try {

   f.delete(); //调用方法,直接删除

  } catch (Exception ex) {

   ex.printStackTrace();

  }  }

 

 

java中删除目录事先要删除目录下的文件或子目录

  • public void del(String filepath) throws IOException{  
  •  File f = new File(filepath);//定义文件路径         
  •  if(f.exists() && f.isDirectory()){//判断是文件还是目录  
  •      if(f.listFiles().length==0){//若目录下没有文件则直接删除  
  •          f.delete();  
  •      }else{//若有则把文件放进数组,并判断是否有下级目录  
  •          File delFile[]=f.listFiles();  
  •          int i =f.listFiles().length;  
  •          for(int j=0;j<i;j++){  
  •              if(delFile[j].isDirectory()){  
  •                        del(delFile[j].getAbsolutePath());//递归调用del方法并取得子目录路径  
  •              }  
  •              delFile[j].delete();//删除文件  
  •          }  
  •      }  
  •  }      
  • }  

 

 

apache开源的包中都包含了这些FileUtils,具体请参考API

File f = new File("D:\\Project Document\\商盟网站p1.01\\bb");
org.apache.commons.io.FileUtils.deleteDirectory(f);//删除目录
org.apache.tools.ant.util.FileUtils.delete(f);//删除文件

 

java.io.File类各种文件操作      

Java代码 复制代码 收藏代码
  1. import java.io.*;  
  2.   
  3. public class FileOperate {  
  4.   public FileOperate() {  
  5.   }  
  6.   
  7.   /** 
  8.    * 新建目录 
  9.    * @param folderPath String 如 c:/fqf 
  10.    * @return boolean 
  11.    */  
  12.   public void newFolder(String folderPath) {  
  13.     try {  
  14.       String filePath = folderPath;  
  15.       filePath = filePath.toString();  
  16.       java.io.File myFilePath = new java.io.File(filePath);  
  17.       if (!myFilePath.exists()) {  
  18.         myFilePath.mkdir();  
  19.       }  
  20.     }  
  21.     catch (Exception e) {  
  22.       System.out.println("新建目录操作出错");  
  23.       e.printStackTrace();  
  24.     }  
  25.   }  
  26.   
  27.   /** 
  28.    * 新建文件 
  29.    * @param filePathAndName String 文件路径及名称 如c:/fqf.txt 
  30.    * @param fileContent String 文件内容 
  31.    * @return boolean 
  32.    */  
  33.   public void newFile(String filePathAndName, String fileContent) {  
  34.   
  35.     try {  
  36.       String filePath = filePathAndName;  
  37.       filePath = filePath.toString();  
  38.       File myFilePath = new File(filePath);  
  39.       if (!myFilePath.exists()) {  
  40.         myFilePath.createNewFile();  
  41.       }  
  42.       FileWriter resultFile = new FileWriter(myFilePath);  
  43.       PrintWriter myFile = new PrintWriter(resultFile);  
  44.       String strContent = fileContent;  
  45.       myFile.println(strContent);  
  46.       resultFile.close();  
  47.   
  48.     }  
  49.     catch (Exception e) {  
  50.       System.out.println("新建目录操作出错");  
  51.       e.printStackTrace();  
  52.   
  53.     }  
  54.   
  55.   }  
  56.   
  57.   /** 
  58.    * 删除文件 
  59.    * @param filePathAndName String 文件路径及名称 如c:/fqf.txt 
  60.    * @param fileContent String 
  61.    * @return boolean 
  62.    */  
  63.   public void delFile(String filePathAndName) {  
  64.     try {  
  65.       String filePath = filePathAndName;  
  66.       filePath = filePath.toString();  
  67.       java.io.File myDelFile = new java.io.File(filePath);  
  68.       myDelFile.delete();  
  69.   
  70.     }  
  71.     catch (Exception e) {  
  72.       System.out.println("删除文件操作出错");  
  73.       e.printStackTrace();  
  74.   
  75.     }  
  76.   
  77.   }  
  78.   
  79.   /** 
  80.    * 删除文件夹 
  81.    * @param filePathAndName String 文件夹路径及名称 如c:/fqf 
  82.    * @param fileContent String 
  83.    * @return boolean 
  84.    */  
  85.   public void delFolder(String folderPath) {  
  86.     try {  
  87.       delAllFile(folderPath); //删除完里面所有内容  
  88.       String filePath = folderPath;  
  89.       filePath = filePath.toString();  
  90.       java.io.File myFilePath = new java.io.File(filePath);  
  91.       myFilePath.delete(); //删除空文件夹  
  92.   
  93.     }  
  94.     catch (Exception e) {  
  95.       System.out.println("删除文件夹操作出错");  
  96.       e.printStackTrace();  
  97.   
  98.     }  
  99.   
  100.   }  
  101.   
  102.   /** 
  103.    * 删除文件夹里面的所有文件 
  104.    * @param path String 文件夹路径 如 c:/fqf 
  105.    */  
  106.   public void delAllFile(String path) {  
  107.     File file = new File(path);  
  108.     if (!file.exists()) {  
  109.       return;  
  110.     }  
  111.     if (!file.isDirectory()) {  
  112.       return;  
  113.     }  
  114.     String[] tempList = file.list();  
  115.     File temp = null;  
  116.     for (int i = 0; i < tempList.length; i++) {  
  117.       if (path.endsWith(File.separator)) {  
  118.         temp = new File(path + tempList[i]);  
  119.       }  
  120.       else {  
  121.         temp = new File(path + File.separator + tempList[i]);  
  122.       }  
  123.       if (temp.isFile()) {  
  124.         temp.delete();  
  125.       }  
  126.       if (temp.isDirectory()) {  
  127.         delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件  
  128.         delFolder(path+"/"+ tempList[i]);//再删除空文件夹  
  129.       }  
  130.     }  
  131.   }  
  132.   
  133.   /** 
  134.    * 复制单个文件 
  135.    * @param oldPath String 原文件路径 如:c:/fqf.txt 
  136.    * @param newPath String 复制后路径 如:f:/fqf.txt 
  137.    * @return boolean 
  138.    */  
  139.   public void copyFile(String oldPath, String newPath) {  
  140.     try {  
  141.       int bytesum = 0;  
  142.       int byteread = 0;  
  143.       File oldfile = new File(oldPath);  
  144.       if (oldfile.exists()) { //文件存在时  
  145.         InputStream inStream = new FileInputStream(oldPath); //读入原文件  
  146.         FileOutputStream fs = new FileOutputStream(newPath);  
  147.         byte[] buffer = new byte[1444];  
  148.         int length;  
  149.         while ( (byteread = inStream.read(buffer)) != -1) {  
  150.           bytesum += byteread; //字节数 文件大小  
  151.           System.out.println(bytesum);  
  152.           fs.write(buffer, 0, byteread);  
  153.         }  
  154.         inStream.close();  
  155.       }  
  156.     }  
  157.     catch (Exception e) {  
  158.       System.out.println("复制单个文件操作出错");  
  159.       e.printStackTrace();  
  160.   
  161.     }  
  162.   
  163.   }  
  164.   
  165.   /** 
  166.    * 复制整个文件夹内容 
  167.    * @param oldPath String 原文件路径 如:c:/fqf 
  168.    * @param newPath String 复制后路径 如:f:/fqf/ff 
  169.    * @return boolean 
  170.    */  
  171.   public void copyFolder(String oldPath, String newPath) {  
  172.   
  173.     try {  
  174.       (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹  
  175.       File a=new File(oldPath);  
  176.       String[] file=a.list();  
  177.       File temp=null;  
  178.       for (int i = 0; i < file.length; i++) {  
  179.         if(oldPath.endsWith(File.separator)){  
  180.           temp=new File(oldPath+file[i]);  
  181.         }  
  182.         else{  
  183.           temp=new File(oldPath+File.separator+file[i]);  
  184.         }  
  185.   
  186.         if(temp.isFile()){  
  187.           FileInputStream input = new FileInputStream(temp);  
  188.           FileOutputStream output = new FileOutputStream(newPath + "/" +  
  189.               (temp.getName()).toString());  
  190.           byte[] b = new byte[1024 * 5];  
  191.           int len;  
  192.           while ( (len = input.read(b)) != -1) {  
  193.             output.write(b, 0, len);  
  194.           }  
  195.           output.flush();  
  196.           output.close();  
  197.           input.close();  
  198.         }  
  199.         if(temp.isDirectory()){//如果是子文件夹  
  200.           copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);  
  201.         }  
  202.       }  
  203.     }  
  204.     catch (Exception e) {  
  205.       System.out.println("复制整个文件夹内容操作出错");  
  206.       e.printStackTrace();  
  207.   
  208.     }  
  209.   
  210.   }  
  211.   
  212.   /** 
  213.    * 移动文件到指定目录 
  214.    * @param oldPath String 如:c:/fqf.txt 
  215.    * @param newPath String 如:d:/fqf.txt 
  216.    */  
  217.   public void moveFile(String oldPath, String newPath) {  
  218.     copyFile(oldPath, newPath);  
  219.     delFile(oldPath);  
  220.   
  221.   }  
  222.   
  223.   /** 
  224.    * 移动文件到指定目录 
  225.    * @param oldPath String 如:c:/fqf.txt 
  226.    * @param newPath String 如:d:/fqf.txt 
  227.    */  
  228.   public void moveFolder(String oldPath, String newPath) {  
  229.     copyFolder(oldPath, newPath);  
  230.     delFolder(oldPath);  
  231.   
  232.   }  
  233. }