闲来无事,写了个删除文件夹的java类
来源:互联网 发布:现在淘宝开店怎么样 编辑:程序博客网 时间:2024/05/01 05:59
作完了设计,闲来无事,同学问我怎么删除文件夹(她的设计是jsp作购物网站),于是就写了个java类File继承自java.io.File,以下是代码,第一这么写,同学们不要见笑,呵呵
/**
* @author www.nwu.edu.cn MiniRice
* @author darkrice@gmail.com
* @version 1.2
*
*/
package cn.edu.nwu.io;
import java.util.*;
public class File extends java.io.File {
File(java.io.File file,String str){
super(file,str);
}
File(String str1,String str2){
super(str1,str2);
}
File(String str){
super(str);
}
File(java.net.URI uri){
super(uri);
}
public static void deleteDirs(java.io.File dir)throws Exception{
m_root=dir;
m_dirs= new ArrayList();
if(!m_root.isDirectory()){
throw new Exception("Exception:/""+m_root.toString()+"/" is not a director");
}
else{
for(int i=0;i<m_dirs.size();i++){
System.out.println(((File)m_dirs.get(i)).toString());
}
//delete all director
try{
m_dirs.add(m_root);
myDelete();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
*
* @param dirPath String a director file path;
* @throws IOException if dirPath is not a director file path
*/
public static void deleteDirs(String dirPath)throws Exception{
m_root=new java.io.File(dirPath);
deleteDirs(m_root);
}
public static void deleteSubDirs(String dirPath)throws Exception{
m_root=new java.io.File(dirPath);
deleteSubDirs(m_root);
}
public static void deleteSubDirs(java.io.File dir)throws Exception{
m_root=dir;
m_dirs= new ArrayList();
//deleteDirs(m_root);
if(!m_root.isDirectory()){
throw new Exception("Exception:/""+m_root.toString()+"/" is not a director");
}
else{
for(int i=0;i<m_dirs.size();i++){
System.out.println(((File)m_dirs.get(i)).toString());
}
try{
myDelete();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/*
* visit all a director and save them in a list
*/
private static void visitAll(java.io.File tempRoot){
//m_dirs.add(tempRoot);
//System.out.println("函数开始处:"+m_dirs.size());
java.io.File[] dirs=tempRoot.listFiles();
if(dirs!=null){
List dirsList=Arrays.asList(dirs);
//while(dirsList!=null){
if(dirsList==null){
try{
tempRoot.delete();
}
catch(Exception e){
e.printStackTrace();
}
}
else{
m_dirs.addAll(dirsList);
//System.out.println(m_dirs.size());
/*
for(int i=0;i<dirs.length;i++){//print dirs
System.out.println("NO."+ ++counter+":"+dirs[i].toString());
}
*/
//System.out.println(m_dirs.size());
for(int i=0;i<dirsList.size();i++){
tempRoot=(java.io.File)dirsList.get(i);
visitAll(tempRoot);
/*
dirs=tempRoot.listFiles();
List dirsList= Arrays.asList(dirs);
*/
//System.out.println(tempRoot.toString());
}
}
//return true;
//}
}
}
/*
* do delete
*/
private static void myDelete()throws Exception{
//m_dirs.add(m_root);
visitAll(m_root);
if(m_dirs!=null){
//System.out.println("删除前:"+m_dirs.size());
//System.out.println(m_dirs.size());
for(int i=m_dirs.size()-1;i>=0;i--){
java.io.File f= (java.io.File)m_dirs.remove(i);
//File f= (File)m_dirs.get(i);
//System.out.println(f.toString());
String fileName=f.toString();
if(!f.delete()){
throw new Exception("Exception: delete file "+ fileName+" false!");
//System.out.println("删除文件/文件夹"+fileName+"失败!");
}
}
}
else{
throw new Exception("Exception: read file list of "+m_root.toString()+"false!");
//System.out.println("ArrayList==null");
}
}
/*
* root director
*/
private static java.io.File m_root;
private static ArrayList m_dirs;
//private static int counter=0;
public static void main(String[] args) {//测试类
File file =new File("J:/毕业设计/测试类/测试文件");
try{
File.deleteDirs("J:/毕业设计/测试类/测试文件");
//File.deleteDirs(file);
//File.deleteSubDirs("J:/毕业设计/测试类/测试文件");
//File.deleteSubDirs(file);
//File f=new File("F:/test.txt");
//f.delete();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("删除成功~");
}
}//:~
完毕,问题倒是不难,就是第一尝试用继承和异常。
/**
* @author www.nwu.edu.cn MiniRice
* @author darkrice@gmail.com
* @version 1.2
*
*/
package cn.edu.nwu.io;
import java.util.*;
public class File extends java.io.File {
File(java.io.File file,String str){
super(file,str);
}
File(String str1,String str2){
super(str1,str2);
}
File(String str){
super(str);
}
File(java.net.URI uri){
super(uri);
}
public static void deleteDirs(java.io.File dir)throws Exception{
m_root=dir;
m_dirs= new ArrayList();
if(!m_root.isDirectory()){
throw new Exception("Exception:/""+m_root.toString()+"/" is not a director");
}
else{
for(int i=0;i<m_dirs.size();i++){
System.out.println(((File)m_dirs.get(i)).toString());
}
//delete all director
try{
m_dirs.add(m_root);
myDelete();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
*
* @param dirPath String a director file path;
* @throws IOException if dirPath is not a director file path
*/
public static void deleteDirs(String dirPath)throws Exception{
m_root=new java.io.File(dirPath);
deleteDirs(m_root);
}
public static void deleteSubDirs(String dirPath)throws Exception{
m_root=new java.io.File(dirPath);
deleteSubDirs(m_root);
}
public static void deleteSubDirs(java.io.File dir)throws Exception{
m_root=dir;
m_dirs= new ArrayList();
//deleteDirs(m_root);
if(!m_root.isDirectory()){
throw new Exception("Exception:/""+m_root.toString()+"/" is not a director");
}
else{
for(int i=0;i<m_dirs.size();i++){
System.out.println(((File)m_dirs.get(i)).toString());
}
try{
myDelete();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/*
* visit all a director and save them in a list
*/
private static void visitAll(java.io.File tempRoot){
//m_dirs.add(tempRoot);
//System.out.println("函数开始处:"+m_dirs.size());
java.io.File[] dirs=tempRoot.listFiles();
if(dirs!=null){
List dirsList=Arrays.asList(dirs);
//while(dirsList!=null){
if(dirsList==null){
try{
tempRoot.delete();
}
catch(Exception e){
e.printStackTrace();
}
}
else{
m_dirs.addAll(dirsList);
//System.out.println(m_dirs.size());
/*
for(int i=0;i<dirs.length;i++){//print dirs
System.out.println("NO."+ ++counter+":"+dirs[i].toString());
}
*/
//System.out.println(m_dirs.size());
for(int i=0;i<dirsList.size();i++){
tempRoot=(java.io.File)dirsList.get(i);
visitAll(tempRoot);
/*
dirs=tempRoot.listFiles();
List dirsList= Arrays.asList(dirs);
*/
//System.out.println(tempRoot.toString());
}
}
//return true;
//}
}
}
/*
* do delete
*/
private static void myDelete()throws Exception{
//m_dirs.add(m_root);
visitAll(m_root);
if(m_dirs!=null){
//System.out.println("删除前:"+m_dirs.size());
//System.out.println(m_dirs.size());
for(int i=m_dirs.size()-1;i>=0;i--){
java.io.File f= (java.io.File)m_dirs.remove(i);
//File f= (File)m_dirs.get(i);
//System.out.println(f.toString());
String fileName=f.toString();
if(!f.delete()){
throw new Exception("Exception: delete file "+ fileName+" false!");
//System.out.println("删除文件/文件夹"+fileName+"失败!");
}
}
}
else{
throw new Exception("Exception: read file list of "+m_root.toString()+"false!");
//System.out.println("ArrayList==null");
}
}
/*
* root director
*/
private static java.io.File m_root;
private static ArrayList m_dirs;
//private static int counter=0;
public static void main(String[] args) {//测试类
File file =new File("J:/毕业设计/测试类/测试文件");
try{
File.deleteDirs("J:/毕业设计/测试类/测试文件");
//File.deleteDirs(file);
//File.deleteSubDirs("J:/毕业设计/测试类/测试文件");
//File.deleteSubDirs(file);
//File f=new File("F:/test.txt");
//f.delete();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("删除成功~");
}
}//:~
完毕,问题倒是不难,就是第一尝试用继承和异常。
- 闲来无事,写了个删除文件夹的java类
- 闲来无事,写了个简单的c#生成静态html文件,封装类
- 闲来无事写了个在线RSS阅读器
- 闲来无事,写了个简易计算器
- 闲来无事,写了一个简单的智能指针
- 闲来无事,写了一个比较完整的计算器
- 闲来无事,用Java的软引用写了一个山寨的缓存
- 闲来无事,将自己收集到的工具类封装了下,做了一个lib,写了个简单的mvp,初学者可以看看
- 闲来无事,写了个房态图,练练好久没有写代码了的手
- 闲来无事,用java写了一个魔方小程序。附源码
- 最近无事,写了个java布局管理器,名为FreedomLayout
- 闲来无事写个PHP5 OOP模型
- 闲来无事,用C写个朴素贝叶斯
- 中秋闲来无事,自己写个2048玩玩
- 闲来无事写个多线程例子总结知识
- Golang1.7闲来无事写了一个基于Gob的tcp通讯用的包
- 闲来无事---写了一个生成验证码的代码
- 闲来无事,写个博客,谈谈移动互联网的趋势!
- VI命令
- UNIX内存占用基本检查
- 同济大学软件学院万院长谈择业
- CSDN的Blog越来越差了
- 关于DataGrid等控件中的自动编号
- 闲来无事,写了个删除文件夹的java类
- ADO.NET 和 ADO 的比较
- what?
- 在什么特殊情况下延长劳动者工作时间可不受限制
- JavaScript通用库(一)(摘)
- Visual C++ 6.0 ( VC 6 )带 SP6 中英文双语版 下载
- JavaScript通用库(二)(摘)
- 高级软件设计师
- 计算机图像处理与分析 (方正图书馆)