博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中对文件的处理01-递归删除
阅读量:6914 次
发布时间:2019-06-27

本文共 4318 字,大约阅读时间需要 14 分钟。

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"));    }}

 

转载于:https://www.cnblogs.com/wang-liang-blogs/p/10620786.html

你可能感兴趣的文章
Linux内存使用情况以及内存泄露分析之工具与方法
查看>>
安装 Ruby, Rails 运行环境
查看>>
Office EXCEL 如何保留一位小数,并且单击这个单元格的时候没有一大串小数
查看>>
Unity ShaderLab学习总结
查看>>
JS 遍历JSON中每个key值
查看>>
一些小功能实现
查看>>
前端学习 -- Css -- 行间距
查看>>
android图像处理(3) 浮雕效果
查看>>
实时视频应用之QoS关键技术分析
查看>>
【Asp.net之旅】--因自己定义控件注冊而引发的思考
查看>>
RAD Studio XE8 技术研讨会讲义与范例程序下载
查看>>
quick-cocos2d-x开发工具sublime text及其强力插件QuickXDev
查看>>
VirtualBox修改现有VDI虚拟磁盘大小
查看>>
mac 10.12 sierra 机械键盘+ratm可编程鼠标记录
查看>>
jmeter用beanshell调用自己写的jar进行MD5加密
查看>>
调用系统相机相冊
查看>>
最简单的视音频播放演示样例7:SDL2播放RGB/YUV
查看>>
vector draw 试用期结束的 激活方法
查看>>
Oracle数据库软件标准版的一个限制:仅仅能用一个rman channel
查看>>
docker官方文档中的dns,link,expose,publish
查看>>