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

Java中对文件的处理01-递归删除的更多相关文章

  1. Java中的文件操作

    在使用计算机编程中,常常会用到对于文件的操作,以下是我对于Java中文件的相关内容学习之后的一个总结和在学习过程中遇到的一些问题. 一.什么是文件 对于文件进行操作,首先我们要知道什么是文件.在此之前 ...

  2. Java中读取文件

    Java中读取文件,去除一些分隔符,保存在多维数组里面 public void readFile(String filePath) { File file=new File(filePath); Ar ...

  3. JAVA中获取文件MD5值的四种方法

    JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...

  4. Java中获取文件路径

    Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...

  5. java中常量文件的配置与读取

    java中常量文件的配置与读取: package com.floor.shop.user.util; import java.io.InputStream; import java.io.InputS ...

  6. Java中的文件操作(一)RandomAccessFile

    今天,学到的是java中的文件操作. Java.IO.File Java中操作文件用到RandomAccessFile类,既可以读取文件内容,也可以向文件输出数据,但不同与普通输入/输出流的是Rand ...

  7. java中把文件拷贝到指定目录下最简单几种方法

    java中把文件拷贝到指定目录下最简单几种方法   String savePath = "D:/file";// 文件保存到d盘的file目录下 File savefile = n ...

  8. Java中移动文件或目录的方法盘点

    本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:Java中移动文件或目录的方法盘点: import org.apache.commons.io.FileUtils; import jav ...

  9. 3,Java中的文件IO流

    1,File类 ··· 概念:File对象可以表示一个文件或目录.可以对其进行增删改查. ··· 常用方法:     File f = new File(".");     判断是 ...

随机推荐

  1. python篇第10天【While 循环语句】

    while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值 ...

  2. dw中几个必须掌握的快捷键

    相信很多初学者,在使用软件制作网页的时候需要去软件操作界面点击按钮来实现编辑,现在给大家分享几个最常用到的快捷方式!这样能让大家在使用中更为方便,节约时间提高工作效率 加粗 Ctrl + B斜体 Ct ...

  3. Javers 比较两个类的差异

    Javers 在开发过程中遇到需求,比较数据库中的原数据与新修改要写入库中的数据.这个实体类是比较复杂的.例如有基本类型,BigDecimal类型,自定义类型,Date类型,List集合,Set集合, ...

  4. 防世界之Web_NewsCenter

    题目:  打开实验环境一看,就一个搜索框,emmm试下有没有SQL注入点,SQL注入步骤传送门https://www.cnblogs.com/shacker/p/15917173.html 爆出数据, ...

  5. 00_UML图形分类

    1.   描述对象 类图(Class Diagram) 包图(Package Diagram) 对象图(Object Diagram) 组件图(Component Diagram) 部署图(Deplo ...

  6. 在 TIME_WAIT 状态的 TCP 连接,收到 SYN 后会发生什么?

    周末跟朋友讨论了一些 TCP 的问题,在查阅<Linux 服务器高性能编程>这本书的时候,发现书上写了这么一句话: 书上说,处于 TIME_WAIT 状态的连接,在收到相同四元组的 SYN ...

  7. 2021年企业bi工具推荐

    数据时代,商业智能工具对于企业了解复杂的大数据非常重要. 我们研究整理了国内外几十个商业智能BI工具,主要就其在功能.性能.价格.体验.安全等方面进行评测,希望帮助企业更好的进行BI产品选型. 一.t ...

  8. LVM--逻辑卷管理@安装、格式化、挂载、开机自动挂载完整篇

    转至:https://blog.51cto.com/xiguatailang/1256606      LVM的重要性在这里我也就不多说了,今天和大家分享一下,LVM的安装方式,以及挂载方式. 首先呢 ...

  9. 按照TomCat版本重新配置web.xml文件

    在TomCat的目录:C:\Program Files\Apache Software Foundation\Tomcat 9.0_Tomcat9.2\webapps\ROOT\WEB-INF下的we ...

  10. Pycharm:设置自带控制台的python版本

    之前在用chr将一个编码转化为对应的字符时,出现以下提示 chr() arg not in range(256) 后来发现,只有python2.x才会出现这种情况,python3.x统一使用unico ...