删除临时文件代码(2012.10.19)

来源:互联网 发布:网络直播原理 编辑:程序博客网 时间:2024/06/05 03:45

package com.lonnesol.tools;

import com.lonnesol.ufk.tools.Loginfo;
import java.io.File;

/**
 *
 * @author sunny

 * @email tec_feng@hotmail.com
 */
public class DeleteFileTemp {

    private static String firstDirectory = "";

    public static void main(String args[]) {
        delClientTemp("C:/test");
        delNetTemp("D:/Project/test/web");
        delAllTemp("D:/Project/test/web", "C:/test");
    }

   
   
     /**
     *
     * @param path 根目录路径
     * @desc 删除net和client端产生的临时文件
     */
    public static void delAllTemp(String netPath,String clientPath){
        delNetTemp(netPath);
        delClientTemp(clientPath);
    }
    /**
     *
     * @param path 根目录路径
     * @desc 删除net端产生的临时文件
     */
    public static void delNetTemp(String path) {
        String relativePath = getPath(path);
        //<editor-fold defaultstate="collapsed" desc="获取要删除文件夹路径">
        String uploadExcelPath = relativePath + "/UploadExcel";
        String excelExportPath = relativePath + "/ExcelExport";
        String reportDocPath = relativePath + "/reportDoc";
        String tcbrPath = relativePath + "/TCBR";
        String forecastExcelExportPath = relativePath + "/ForecastExcelExport";
        String tempPath = relativePath + "/temp";
        String excelExportForRollPath = relativePath + "/ExcelExportForRoll";
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="需要保留的模版文件">
        String[] uploadExcelRetain = {""};
        String[] excelExportRetain = {"export.xls","update.xls"};
        String[] reportDocRetain = {""};
        String[] tcbrRetain = {""};
        String[] forecastExcelExportRetain = {"Cond-GIIP.xls",
            "Cond-GIIP-Inj.xls", "Cond-URR.xls", "Cond-URR-Inj.xls",
            "Gas-GIIP.xls", "Gas-URR.xls", "Oil-STOIIP.xls", "Oil-URR.xls"};
        String[] tempRetain = {"temp.loc"};
        String[] excelExportForRollRetain = {"RollupSummary.xls"};
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="清除文件">
        Loginfo.log.info("clear folder uploadExcel");
        firstDirectory = uploadExcelPath;
        delFolder(uploadExcelPath, uploadExcelRetain);
        Loginfo.log.info("clear folder excelExport");
        firstDirectory = excelExportPath;
        delFolder(excelExportPath, excelExportRetain);
        Loginfo.log.info("clear folder reportDoc");
        firstDirectory = reportDocPath;
        delFolder(reportDocPath, reportDocRetain);
        Loginfo.log.info("clear folder tcbr");
        firstDirectory = tcbrPath;
        delFolder(tcbrPath, tcbrRetain);
        Loginfo.log.info("clear folder forecastExcelExport");
        firstDirectory = forecastExcelExportPath;
        delFolder(forecastExcelExportPath, forecastExcelExportRetain);
        Loginfo.log.info("clear folder temp");
        firstDirectory = tempPath;
        delFolder(tempPath, tempRetain);
        Loginfo.log.info("clear folder excelExportForRoll");
        firstDirectory = excelExportForRollPath;
        delFolder(excelExportForRollPath, excelExportForRollRetain);
        //</editor-fold>

    }
   
    /**
     *
     * @param path 根目录路径
     * @desc 删除client端产生的临时文件
     */
    public static void delClientTemp(String path) {
        String relativePath = path;
        //<editor-fold defaultstate="collapsed" desc="获取要删除文件夹路径">
        String uploadExcelPath = relativePath + "/UploadExcel";
        String excelExportPath = relativePath + "/ExcelExport";
        String figurePath = relativePath + "/Figure";
        String forecastExcelExportPath = relativePath + "/ForecastExcelExport";
        String templatePath = relativePath +"/Template";
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="需要保留的模版文件">
        String[] uploadExcelRetain = {""};
        String[] excelExportRetain = {""};
        String[] figureRetain = {""};
        String[] forecastExcelExportRetain = {""};
        String[] templateRetain = {"test.txt"."test2.txt"};
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="清除文件">
        Loginfo.log.info("clear folder uploadExcel");
        firstDirectory = uploadExcelPath;
        delFolder(uploadExcelPath, uploadExcelRetain);
        Loginfo.log.info("clear folder excelExport");
        firstDirectory = excelExportPath;
        delFolder(excelExportPath, excelExportRetain);
        Loginfo.log.info("clear figure excelExport");
        firstDirectory = figurePath;
        delFolder(figurePath, figureRetain);
        Loginfo.log.info("clear folder forecastExcelExport");
        firstDirectory = forecastExcelExportPath;
        delFolder(forecastExcelExportPath, forecastExcelExportRetain);
        Loginfo.log.info("clear folder Template");
        firstDirectory = templatePath;
        delFolder(templatePath, templateRetain);
        //</editor-fold>


    }

    private static String getPath(String folderpath) {
        String relPath = "";
        if (folderpath.contains("/")) {
            relPath = folderpath.substring(0, folderpath.lastIndexOf("/"));
        }
        String relativePath = relPath + "/build/web";
        return relativePath;
    }
   
    /**
     *
     * @param folderPath Delete folder path
     * @param retainFileName The need to retain the name of the file
     */
    private static void delFolder(String folderPath, String[] retainFileName) {
        if (retainFileName == null) {
            retainFileName = new String[]{""};
        }
        delFile(folderPath, retainFileName);
        String filePath = folderPath;
        filePath = filePath.toString();
        File myFilePath = new File(filePath);
        if (!firstDirectory.equals(folderPath)) {
            myFilePath.delete();
        }
    }

    private static boolean delFile(String folderPath, String[] retainFileName) {
        boolean flag = false;
        File file = new File(folderPath);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        StringBuffer retainFileNameStr = new StringBuffer();
        for (String fileName : retainFileName) {
            retainFileNameStr.append((fileName + "|"));
        }
        String retainFileStr = retainFileNameStr.toString();
        //<editor-fold defaultstate="collapsed" desc="删除文件夹或文件">
        for (int i = 0; i < tempList.length; i++) {
            // keep the retain file
            if (retainFileStr.contains(tempList[i])) {
                continue;
            }
            if (folderPath.endsWith("/")) {
                temp = new File(folderPath + tempList[i]);
            } else {
                temp = new File(folderPath + "/" + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delFile(folderPath + "/" + tempList[i], retainFileName);
                delFolder(folderPath + "/" + tempList[i], retainFileName);
                flag = true;
            }
        }
        //</editor-fold>
        return true;

    }
}