Java删除文件夹 及目录下 文件 递归操作
2013-12-19 14:47
671 查看
public class FileOpeUtil {
public static void main(String[] args) throws IOException {
// delAllFiles("C://Users/13075012/.swt/");
String s = "D://opt/opt/ds.json";
/*
* String name1 = s.substring(0,s.lastIndexOf("/")+1); System.out.println(name1); String name =
* s.substring(s.lastIndexOf("/")+1); System.out.println(name); File f = new File(name1); if(!f.exists()){
* f.mkdirs(); }
*/
File f2 = new File(s);
System.out.println(f2.createNewFile());
}
public static void dealWithDir(String prefixPath, String lockerCode, boolean flag) {
delAllFiles(prefixPath);
if (flag) {
File file = new File(prefixPath + lockerCode);
file.mkdir();
}
}
private static void delAllFiles(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.delete()) {
delAllFolders(file.getPath());
} else {
file.delete();
}
}
private static void delAllFolders(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
delAllFiles(f.getPath());
delAllFolders(f.getPath());
f.delete();
}
}
if (file.isFile()) {
file.delete();
}
}
}
============================================
String absolutePath = request.getSession().getServletContext()
.getRealPath(File.separator + "software" + File.separator + "updatelist.json");
File jsonFile = new File(absolutePath);
if (!jsonFile.exists()) {
String absuluDir = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator) + 1);
File dirFile = new File(absuluDir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
jsonFile.createNewFile();
}
br = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile)));
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
}
JSONObject jsonObj = JsonFileUtil.dealWithUpdate(stringBuffer.toString(), lockerCode, delFlag,
orginFileName);
// 将json数据再次写入文件
bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File(
absolutePath)))));
bw.write(jsonObj.toString());
bw.flush();// 强制将缓冲区 写入
public static void main(String[] args) throws IOException {
// delAllFiles("C://Users/13075012/.swt/");
String s = "D://opt/opt/ds.json";
/*
* String name1 = s.substring(0,s.lastIndexOf("/")+1); System.out.println(name1); String name =
* s.substring(s.lastIndexOf("/")+1); System.out.println(name); File f = new File(name1); if(!f.exists()){
* f.mkdirs(); }
*/
File f2 = new File(s);
System.out.println(f2.createNewFile());
}
public static void dealWithDir(String prefixPath, String lockerCode, boolean flag) {
delAllFiles(prefixPath);
if (flag) {
File file = new File(prefixPath + lockerCode);
file.mkdir();
}
}
private static void delAllFiles(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.delete()) {
delAllFolders(file.getPath());
} else {
file.delete();
}
}
private static void delAllFolders(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
delAllFiles(f.getPath());
delAllFolders(f.getPath());
f.delete();
}
}
if (file.isFile()) {
file.delete();
}
}
}
============================================
String absolutePath = request.getSession().getServletContext()
.getRealPath(File.separator + "software" + File.separator + "updatelist.json");
File jsonFile = new File(absolutePath);
if (!jsonFile.exists()) {
String absuluDir = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator) + 1);
File dirFile = new File(absuluDir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
jsonFile.createNewFile();
}
br = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile)));
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
}
JSONObject jsonObj = JsonFileUtil.dealWithUpdate(stringBuffer.toString(), lockerCode, delFlag,
orginFileName);
// 将json数据再次写入文件
bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File(
absolutePath)))));
bw.write(jsonObj.toString());
bw.flush();// 强制将缓冲区 写入
相关文章推荐
- Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录
- 【转】C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- java单个文件复制,递归删除、复制目录下所有内容,递归创建文件夹,.7z文件解压等
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C#文件操作源码 递归复制一个目录 递归删除文件 和 文件夹
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C#文件操作源码 递归复制一个目录 递归删除文件 和 文件夹
- java中文件操作(递归删除空文件夹,删除过期文件)
- 转:C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件
- java递归删除文件夹demo,删除文件目录例子
- java文件操作整理(一)--递归删除目录和文件的程序
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件转http://www.cnblogs.com/zhuzhiyuan/archive/2011/04/22/2024485.html
- 【转载】C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- java文件操作整理(一)--递归删除目录和文件的程序