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("."); 判断是 ...
随机推荐
- uniapp 微信发送订阅消息
这篇主要针对小程序进行演示,既然是发送消息,那么就有三个问题.发送什么内容,给谁发送,怎么发送!往下一条一条解决. 发送什么消息内容 - 通过微信公众号平台 选择对应的消息模板 选择以后在我的模板里面 ...
- 前端程序员初步认识 docker
初步认识 docker 为什么要学习 docker 有同学说他开发工作中有两大神器,一个是 vim 编辑器,另一个就是 Docker. 什么是 docker Docker 是一个开源的应用容器引擎. ...
- ASP.NET Core 6框架揭秘实例演示[08]:配置的基本编程模式
.NET的配置支持多样化的数据源,我们可以采用内存的变量.环境变量.命令行参数.以及各种格式的配置文件作为配置的数据来源.在对配置系统进行系统介绍之前,我们通过几个简单的实例演示一下如何将具有不同来源 ...
- Python数据分析 | Numpy与1维数组操作
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/33 本文地址:http://www.showmeai.tech/article-det ...
- 【论文考古】分布式优化 Communication Complexity of Convex Optimization
J. N. Tsitsiklis and Z.-Q. Luo, "Communication complexity of convex optimization," Journal ...
- vivo全球商城全球化演进之路——多语言解决方案
一.背景 随着经济全球化的深入,许多中国品牌纷纷开始在海外市场开疆扩土.实现全球化意味着你的产品或者应用需要能够在全球各地的语言环境使用,我们在进行海外业务的推进时,需要面对的最大挑战就是多语言问题. ...
- Linux安装ms-office
https://ittutorials.net/open-source/linux/installing-microsoft-office-in-ubuntu/
- [BACKUP] Visual Studio Code 配置
0 VSCode 便携模式:https://code.visualstudio.com/docs/editor/portable#_enable-portable-mode 1. 字体 FiraCod ...
- 如何将csf ip 端口映射
csf基于iptables的,不支持SNAT外,其他映射是支持的.最新版不知道,有没有解决这个问题.配置文件/etc/csf.allow 1,DNAT 用法: IPx|*|IPy|*|tcp/udp ...
- 彻底关闭 win10家庭版 杀毒软件windows defender
下面开始今天的教程, 第一步,我们先在windows安全中心将相关的设置关闭一下,具体方法如下: 我们右键点击windows 10开始菜单,点击"设置", 点击"设置&q ...