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("."); 判断是 ...
随机推荐
- sublime与python交互
点击菜单栏中的工具 -> 编译系统,勾选Python即可 创建hello.py文件,Ctrl+S保存文件,Ctrl+B执行文件,结果如下图 3.sublime运行python文件的交互环境设 ...
- nginx 配置文件与日志模块
内容概要 Nginx 虚拟主机 基于多 IP 的方式 基于多端口的方式 基于多域名的方式 日志配置(日志格式详解) Nginx 访问控制模块(可以去 Nginx.org 文档 documentatio ...
- Solution -「多校联训」朝鲜时蔬
\(\mathcal{Description}\) Link. 破案了,朝鲜时蔬 = 超现实树!(指写得像那什么一样的题面. 对于整数集 \(X\),定义其 好子集 为满足 \(Y\sub ...
- [LeetCode]3.无重复字符的最长子串(Java)
原题地址: longest-substring-without-repeating-characters/submissions 题目描述: 示例 1: 输入: s = "pwwkew&qu ...
- python 定时任务apscheduler的使用
apscheduler 的使用 我们项目中总是避免不了要使用一些定时任务,比如说最近的项目,用户点击报名考试以后需要在考试日期临近的时候推送小程序消息提醒到客户微信上,翻了翻 fastapi 中的 ...
- 花里胡哨之自定义linux终端前缀显示
文章目录 1.先看默认的linux终端前缀 2.查看默认的终端前缀变量 3.符号所代表的意义 4.修改PS1变量,达成自定义效果 4.1.只显示主机名和完整目录 4.2.给他点颜色看看 5.谢幕 1. ...
- 【Java分享客栈】我为什么极力推荐XXL-JOB作为中小厂的分布式任务调度平台
前言 大家好,我是福隆苑居士,今天给大家聊聊XXL-JOB的使用. XXL-JOB是本人呆过的三家公司都使用到的分布式任务调度平台,前两家都是服务于传统行业(某大型移动基地和某大型电网),现在 ...
- [转自Matrix67] 趣题:顶点数为多少的图有可能和自己互补
若干个顶点以及某些顶点和顶点之间的连线,就构成了一个"图".如果对某个图进行变换,使得原来任意两个有连线的顶点之间都不再有连线,原来任意两个没有连线的顶点之间现在都有连线了,那么所 ...
- 人工智能进度好慢,呜呜呜 还是先来学python!!!!!!!二十三讲、二十四讲!
0:十转2 def my_bin(x): a ='' if x : a = my_bin(x//2) return a+str(x%2) else: a+='0b' return a print(my ...
- 可视化BI软件为企业提升数据分析效率
可视化BI软件经过几十年的不断发展,已成为大型企业进行商业决策不可缺少的工具.在BI软件问世之前,由于做数据分析可视化的时间较长.人力成本较高,企业一直处于忽视的状态.可视化BI软件的出现极大地提高 ...