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")); } }
Java中对文件的处理01-递归删除的更多相关文章
- Java中的文件操作
在使用计算机编程中,常常会用到对于文件的操作,以下是我对于Java中文件的相关内容学习之后的一个总结和在学习过程中遇到的一些问题. 一.什么是文件 对于文件进行操作,首先我们要知道什么是文件.在此之前 ...
- Java中读取文件
Java中读取文件,去除一些分隔符,保存在多维数组里面 public void readFile(String filePath) { File file=new File(filePath); Ar ...
- JAVA中获取文件MD5值的四种方法
JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...
- Java中获取文件路径
Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...
- java中常量文件的配置与读取
java中常量文件的配置与读取: package com.floor.shop.user.util; import java.io.InputStream; import java.io.InputS ...
- Java中的文件操作(一)RandomAccessFile
今天,学到的是java中的文件操作. Java.IO.File Java中操作文件用到RandomAccessFile类,既可以读取文件内容,也可以向文件输出数据,但不同与普通输入/输出流的是Rand ...
- java中把文件拷贝到指定目录下最简单几种方法
java中把文件拷贝到指定目录下最简单几种方法 String savePath = "D:/file";// 文件保存到d盘的file目录下 File savefile = n ...
- Java中移动文件或目录的方法盘点
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:Java中移动文件或目录的方法盘点: import org.apache.commons.io.FileUtils; import jav ...
- 3,Java中的文件IO流
1,File类 ··· 概念:File对象可以表示一个文件或目录.可以对其进行增删改查. ··· 常用方法: File f = new File("."); 判断是 ...
随机推荐
- 降维处理PCA
要理解什么是降维,书上给出了一个很好但是有点抽象的例子. 说,看电视的时候屏幕上有成百上千万的像素点,那么其实每个画面都是一个上千万维度的数据:但是我们在观看的时候大脑自动把电视里面的场景放在我们所能 ...
- Note -「Mobius 反演」光速入门
目录 Preface 数论函数 积性函数 Dirichlet 卷积 Dirichlet 卷积中的特殊函数 Mobius 函数 & Mobius 反演 Mobius 函数 Mobius 反演 基 ...
- Solution -「CF 1370F2」The Hidden Pair (Hard Version)
\(\mathcal{Description}\) Link (hard) & Link (easy). 这是一道交互题. 给定一棵 \(n\) 个结点的树,其中有两个是特殊结点. ...
- Spring高级特性之一: Aware之ApplicationContextAware
关于Spring Aware之前似乎涉及过,但是未写过单独的小作文.这里为它专门写个小作文吧,关于它在自定义注解中的应用. 首先,概要介绍下Aware. Spring的依赖注入的最大亮点就是你所有的B ...
- 当gitlab的数据库坏了,或者其他的组件坏了,修复教程。
一般企业的gitlab都承载着多个项目的源码和提交记录 如果gitlab的数据库 PostgreSQL 坏掉了,基本很难修复,那这是不是意味着源码丢失了呢. 本文章只针对 gitlab传统存储方式的修 ...
- 关于iOS APP转让
需要以下条件即可
- 设计DFA接受{0,1}上的字符串ω,且ω是3倍数的二进制表示
DFA设计 设计DFA接受{0,1}上的字符串ω,且ω是3倍数的二进制表示 先叙述下思路: 要想证明某数是3的倍数可以让其除以3看余数是否为零即可,现在我们的问题就是如何计算一串二进制数除以3所得的余 ...
- 网络中delay和latency的区别
时序分析基本概念是Latency, 时钟传播延迟.主要指从Clock源到时序组件Clock输入端的延迟时间.它可以分为两个部分:时钟源插入延迟(source latency)和时钟网络延迟(netwo ...
- shell中echo基础及高级用法详解-渐入佳境
--作者:飞翔的小胖猪 --创建时间:2021年2月19日 1.1 基础用法 echo命令用来输出文本,在shell脚本中用来输出提示信息用的比较多. 单引号:原样输出所有的内容,不用转义就能输出特殊 ...
- Linux主机内存评估手册-从零到无
--时间:2020年10月22日 --作者:飞翔的小胖猪 文档基于Centos Linux操作系统作为生产服务器运行环境.实时的查看分析当前系统内存的使用情况是否存在内存瓶颈,结合应用及现行业务需求分 ...