Java中对文件的处理01-递归删除

package com.ricoh.rapp.ezcx.admintoolweb.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.prefs.Preferences;

import com.ricoh.rapp.ezcx.edcactivation.util.ActivationConsts;

public class FileUtil {
    /**
     * 递归删除文件或文件目录
     */
    public static boolean deleteDir(File file) {
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                deleteDir(f);
            }
        }
        return file.delete();
    }

    /**
     * 复制多个或单个文件
     * 
     * @param sourcePath
     *            源文件或文件夹路径
     * @param newPath
     *            复制的新文件路径
     */
    @SuppressWarnings("static-access")
    public static void copyDir(String sourcePath, String newPath) throws IOException {
        File file = new File(sourcePath);
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdirs();
        }

        for (int i = 0; i < filePath.length; i++) {
            if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
                copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
            }
            if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
                copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
            }

        }
    }

    /**
     * 复制文件
     * 
     * @param oldPath
     *            源文件路径
     * @param newPath
     *            复制的新文件路径
     */
    public static void copyFile(String oldPath, String newPath) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(oldPath));
            out = new BufferedOutputStream(new FileOutputStream(newPath));
            byte[] buffer = new byte[4096];
            int readByte = 0;
            while ((readByte = in.read(buffer)) != -1) {
                out.write(buffer, 0, readByte);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void main(String[] args) {
        /*
         * String path = "d:"+File.separator+"wangliang"; System.out.println(path);
         * System.out.println(createDir("D:\\wangliang"));
         * System.out.println(deleteDir("D:\\wangliang"));
         */

        // System.out.println(getEzChargerInstallPath());

        /*
         * String sourcePath = FileUtil.getEzChargerInstallPath(); String logPath =
         * sourcePath + File.separator + "log"; String tomcatLogPath = sourcePath +
         * File.separator + "core" + File.separator + "tomcat" + File.separator +
         * "logs"; String confPath = sourcePath + File.separator + "conf";
         * 
         * String newPath = FileUtil.getEzChargerInstallPath()+ File.separator
         * +"wltemp"+ File.separator + "ServerLogs"; String newLogPath = newPath +
         * File.separator + "log"; String newTomcatLogPath = newPath + File.separator +
         * "tomcatlogs"; String newConfPath = newPath + File.separator + "conf"; try {
         * FileUtil.copyDir(logPath, newLogPath); FileUtil.copyDir(tomcatLogPath,
         * newTomcatLogPath); FileUtil.copyDir(confPath, newConfPath); } catch
         * (IOException e) { FileUtil.deleteDir(new File(newPath)); e.printStackTrace();
         * }
         */

        /*
         * String path = FileUtil.getEzChargerInstallPath()+ File.separator + "wltemp";
         * System.out.println(path); System.out.println(deleteDir(new File(path)));
         */

        // deleteDir(new File("d:/zipfile"));

    }

}

 

posted @ 2019-03-29 13:46  话祥  阅读(327)  评论(0编辑  收藏  举报