2013年8月2日星期五

java.IO.File class iteration Delete Files

 

what is about the File class operation , mainly delete files as an example :

 
  
package com.IODemo; 

import java.io.File;

public class DeleteFile {

/**
* 删除文件或空文件夹
*
@param file
*
@return
*
@throws Exception
*/
public static boolean deleteFile(String path)throws Exception{
File file
= new File(path);
boolean flag = false;
if(file.isFile()){
flag
= file.delete();
}
if(file.isDirectory()){
if(file.listFiles().length <= 0){
flag
= file.delete();
}
}
return flag;
}

/**
* 删除文件夹下的所有文件
*
@param path
*
@throws Exception
*/
public static void deleteDir(String path)throws Exception{
File file
= new File(path);
File[] files
= null;
if(file.isFile()){
file.delete();
}
else{
files
= file.listFiles();
for (int i = 0; i < files.length; i++) {
//System.out.println(files[i].getPath());
deleteDir(files[i].getPath());
files[i].delete();
}
file.delete();
}
}

public static void main(String[] args)throws Exception{
//System.out.println(deleteFile(new File("d:1.txt")));
//System.out.println(new File("d:/demo").getName());
deleteDir("d:/html");
}
}
 
 

没有评论:

发表评论