用java实现删除文件夹里的所有文件
package com.org.improve.contact;
import java.io.File;
public class DeletePaper {
/**
* @param args
* 用java删除文件夹里的所有文件
*/
public static void main(String[] args) {
DeletePaper dp=new DeletePaper();
delFolder("f:/aa");
System.out.println("此文件夹删除成功!");
}
/**
* 删除文件夹
* param folderPath 文件夹完整绝对路径
*/
public static void delFolder(String folderPath){
try {
deAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete();// 删除空文件夹
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param path删除指定文件夹下所有文件
* @return 文件夹完整绝对路径
*/
private static boolean deAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {// exists() 测试此抽象路径名表示的文件或目录是否存在。
return flag;
}
if (!file.isDirectory()) {// isDirectory() 测试此抽象路径名表示的文件是否是一个目录。
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
deAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}
}
用java实现删除文件夹里的所有文件的更多相关文章
- Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 将Temporary文件夹里的Logo文件转移到Logo文件夹
/// <summary> /// 将Temporary文件夹里的Logo文件转移到Logo文件夹 /// </summary> /// <param name=&quo ...
- Matlab 读取文件夹里所有的文件
(image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...
- 用java删除文件夹里的所有文件
import java.io.File; public class Test { public static void main(String args[]){ Test t = new Test() ...
- php中如何上传整个文件夹里的所有文件?
1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...
- C++实现:把一个文件夹里的冗余文件(.txt)删除
代码很简单,调用了MFC里的几个函数.这里的冗余判断,是要遍历文件内容,进行两两比较. 需要注意的地方有两点: 1.源文件里头文件<afx.h>必须放在最前面.这里是为了避免nafxcwd ...
- Android中res/layout文件夹里新建布局文件,R中不生成ID的奇葩错误
新浪微博:http://weibo.com/u/1928100503 网上看了下,发现大都是xml文件名大写而导致的id不能生成的问题,但在下的问题却不是大小写的问题,在下发现,当你的layout目录 ...
- diff两个文件夹里的东西
diff --help -x, --exclude=PAT exclude files that match PAT 排除某个类型的文件 -u, -U NUM, --uni ...
- jq和js插件的各个文件夹里放置的内容
1. demo文件夹,存放各种实例. 2. dist文件夹,全称是distribution.在某些框架中,因为开发和发布的内容或者代码形式是不一样的(比如利用Grunt压缩等等),这时候就需要一个存放 ...
随机推荐
- WPF之MVVM模式(2)
我们都想追求完美 Every view in the app has an empty codebehind file, except for the standard boilerplate cod ...
- MVC小记备忘
1,页面用<dl><dt><dd>和Bootstrap的"dl-horizontal"类布局页面,使每一个属性名和属性值占一行 <dl c ...
- 控制某个panel的display样式
"我想在onload方法里把panel的 style 里的 display 属性变成 none.我的页面由于有一些脚本,触发某些事件之后还想显示这个panel,不想用Panel3.Visib ...
- 1003. Check If Word Is Valid After Substitutions
We are given that the string "abc" is valid. From any valid string V, we may split V into ...
- DP【洛谷P3089】 [USACO13NOV]POGO的牛Pogo-Cow
[洛谷P3089] [USACO13NOV]POGO的牛Pogo-Cow FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度. FJ觉得让贝西在一条直线的一维线路 ...
- kubernetes相关命令
关闭防火墙 [关闭swap] 执行swapoff -a可临时关闭,但系统重启后恢复 编辑/etc/fstab,注释掉包含swap的那一行即可永久关闭 [关闭SeLinux] sed -i 's/SEL ...
- 【笔记】如何在for语句中迭代多个可迭代对象
并行=>使用内置函数zip,它能将多个可迭代对象合并,每次迭代返回一个元组. for i,j,k in zip(a,b,c): TODO 穿行=>使用标准库中的itertools.chai ...
- Ubuntu系统升级遇到问题记录
The upgrade needs a total of 99.7 M free space on disk '/boot'. Please free at least an additional 5 ...
- 江西财经大学第一届程序设计竞赛 H
链接:https://www.nowcoder.com/acm/contest/115/H来源:牛客网 题目描述 晚上,小P喜欢在寝室里一个个静静的学习或者思考,享受自由自在的单身生活. 他总是能从所 ...
- Codeforces Round #340 (Div. 2) C
Description A flowerbed has many flowers and two fountains. You can adjust the water pressure and se ...