package com.wj;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
    public FileUtil() {
    }
    private static List<String> list = new ArrayList<String>();//所有的后缀为.js,.css,.jsp,.html的文件地址
    private static List<String> list1 = new ArrayList<String>();//所有指定后缀的文件地址

    public static void main(String[] args) {
        try {
            //全部文件
            readfile("E:\\work\\svn\\parent-cp\\trunk\\sm-front-cp\\src\\main\\webapp\\");
            writeFile(list, "E:\\all.txt");
            //指定文件
            readfile2("E:\\work\\svn\\parent-cp\\trunk\\sm-front-cp\\src\\main\\webapp\\", 3);
            writeFile(list1, "E:\\图片.txt");
            deleteAllNoUseJs2("E:\\未使用的图片名称.txt", false);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }    

    /**
     * 删除项目中未引用的文件 
     * @param fileName 把未使用的文件名称生成到文本
     * @param isDelete 是否执行删除操作
     * @author wangjian
     * @date 2018年9月19日 上午10:05:27
     */
    private static void deleteAllNoUseJs2(String fileName, boolean isDelete) {
        List<String> lsit = new ArrayList<String>();
        for (String s : list1) {
            String content =  s.substring(s.lastIndexOf("\\")+1);
            System.out.println("原文件=="+content);
            boolean flag = false;
            for (String ss : list) {
                try {
                    FileReader fr = new FileReader(ss);
                    @SuppressWarnings("resource")
                    BufferedReader br = new BufferedReader(fr);
                    String temp = "";// 用于临时保存每次读取的内容
                    while (temp != null) {

                        temp = br.readLine();
                        if (temp != null && temp.contains(content)) {
                            System.out.println("找到已使用的文件=  " + content);
                            flag = true;
                            break;
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!flag) {
                System.out.println("------------没有使用的文件路径" + s);
                File file = new File(s);
                if (isDelete) {
                    file.delete();
                }
                System.out.println("将要删除的文件=  "+file.getName());
                lsit.add(s);
            }
        }
        writeFile(lsit, fileName);
    } 

    /**
     * 找文件夹下面所有的后缀为.js,.css,.jsp,.html的文件
     * @param filepath
     * @throws FileNotFoundException
     * @throws IOException
     * @author wangjian
     * @date 2018年9月19日 上午10:07:44
     */
    public static void readfile(String filepath)
            throws FileNotFoundException, IOException {
        try {

            File file = new File(filepath);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path==========" + file.getPath());
                System.out.println("absolutepath==========="
                        + file.getAbsolutePath());
                System.out.println("name===========" + file.getName());

            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        String filename = readfile.getName();
                        String lastname = filename.substring(filename
                                .lastIndexOf("."));
                        if (lastname.equals(".css") 
                                || lastname.equals(".jsp")
                                || lastname.equals(".html")
                                || lastname.equals(".js")
                                ) {
                            String realpath = readfile.getPath();
                            list.add(realpath);
                        }

                    } else if (readfile.isDirectory()) {
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
    }
    
    /**
     * 找文件夹下面指定后缀名的文件
     * @param filepath
     * @param type 1、js 2、css 3、图片
     * @throws FileNotFoundException
     * @throws IOException
     * @author wangjian
     * @date 2018年9月19日 上午10:08:04
     */
    public static void readfile2(String filepath, int type)
            throws FileNotFoundException, IOException {
        try {

            File file = new File(filepath);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path==========" + file.getPath());
                System.out.println("absolutepath==========="
                        + file.getAbsolutePath());
                System.out.println("name===========" + file.getName());

            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        String filename = readfile.getName();
                        String lastname = filename.substring(filename
                                .lastIndexOf("."));
                        if (type == 1 && lastname.equals(".js") && !lastname.equals(".json") && !lastname.equals(".jsp")) {
                            String realpath = readfile.getPath();
                            System.out.println("path=" + readfile.getPath());
                            list1.add(realpath);
                        } else if (type == 2 && lastname.equals(".css")) {
                            String realpath = readfile.getPath();
                            System.out.println("path=" + readfile.getPath());
                            list1.add(realpath);
                        }else if (type == 3 && (lastname.equals(".gif") || lastname.equals(".png") || lastname.equals(".jpg"))) {
                            String realpath = readfile.getPath();
                            System.out.println("path=" + readfile.getPath());
                            list1.add(realpath);
                        }


                    } else if (readfile.isDirectory()) {
                        readfile2(filepath + "\\" + filelist[i], type);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("readfile2()   Exception:" + e.getMessage());
        }
    }


    /**
     * 把list记录打印出来
     * @param list
     * @param fileName
     * @author wangjian
     * @date 2018年9月19日 上午10:13:05
     */
    private static void writeFile(List<String> list, String fileName){
        StringBuilder sb = new StringBuilder();
        for(String s : list){
            sb.append(s+"\r\n");
        }
        FileWriter fw = null;
        PrintWriter pw = null;
        try {
            fw = new FileWriter(fileName);
            pw = new PrintWriter(fw);
            pw.println(sb.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if (null != pw) {
                pw.close();
            } 
            
            if (null != fw) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } 
        }
    }

}