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("."); 判断是 ...
随机推荐
- 深度学习:多层感知机和异或问题(Pytorch实现)
感知机模型 假设输入空间\(\mathcal{X}\subseteq \textbf{R}^n\),输出空间是\(\mathcal{Y}=\{-1,+1\}\).输入\(\textbf{x}\in \ ...
- 关于基于GDAL库QT软件平台下C++语言开发使用说明
背景前提 地理空间数据抽象库(GDAL)是一个用于读取和编写栅格和矢量地理空间数据格式的计算机软件库,由开源地理空间基金会在许可的X / MIT风格免费软件许可下发布. 作为一个库,它为调用应用程序提 ...
- 36、python并发编程之多线程(操作篇)
目录: 一 threading模块介绍 二 开启线程的两种方式 三 在一个进程下开启多个线程与在一个进程下开启多个子进程的区别 四 练习 五 线程相关的其他方法 六 守护线程 七 Python GIL ...
- opencv笔记--HOGDescriptor
特征描述提取图像区域上有用信息而忽略无用信息,不同目标下有用信息与无用信息定义不同.这里提取的有用信息用于分类器输入并期望产生正确的分类. HOG(Histogram of Oriented Grad ...
- pycharm软件安装
目前热门的编程软件: 1.VScode 小巧轻便但是对小白不是很优化 2.sublime 时下最流行的代码编辑器,功能十分强大可以运行在windows.macOS和linux系统中,小白先不要使用 3 ...
- XStream: Stream Processing Platform at Facebook
这是Facebook在FlinkForward2021上的一个talk, 主题如下 在前面的论文中分析了Facebook的实时计算引擎的设计和选型的考量,里面提到了Facebook的实时计算引擎为了满 ...
- [源码解析] NVIDIA HugeCTR,GPU 版本参数服务器---(7) ---Distributed Hash之前向传播
[源码解析] NVIDIA HugeCTR,GPU 版本参数服务器---(7) ---Distributed Hash之前向传播 目录 [源码解析] NVIDIA HugeCTR,GPU 版本参数服务 ...
- 把SQLAlchemy查询对象转换成字典
1-假设查出来的为单个对象 1-1 在model.py中为模型对象添加字典转换函数: from exts import db class User(db.Model): __tablename__ = ...
- ISISv4协议测试——网络测试仪实操
文章关键词 ISIS协议:路由协议:协议测试: 一.文章简介: isis是一种与ospf很相似的网络协议(属于动态路由协议),它被应用在巨大规模网络,如运营商以及银行等.同样的它也是基于链路状态算法, ...
- VUE学习笔记(李天禹老师版本)
目录 VUE 一 脚手架文件结构 二 关于不同版本的Vue 三 vue.config.js配置文件 vue文件的基本结构 components 四 ref属性 TIPS 五 props配置项 Tips ...