最近遇到一个问题,就是我的mail文件夹下面的mail太多了,使得Becky的浏览速度变得很慢,所以必须先备份原先的数据,但是又不能将目录结构删除,所以我想写一个java程序来完成这个工作。
刚开始我的思路是用java中出需要删除的文件或文件夹,保存在一个set中,然后对这个set进行遍历,每一项拼成一个DOS命令,然后用Runtime.getRuntime().exec()来运行,思路是对的,但是谁知道当要删除的文件很多的时候,Runtime.getRuntime().exec()产生的进程就会挂起,一直没有响应,我很郁闷,不知道是为什么,上网找资料也没有找到,所以另辟蹊径了。
将所有的命令输出到一个bat文件中,然后用Runtime.getRuntime().exec()来调用这个文件就解决问题了。
源代码如下:
有一些冗余的东西没有删除。
当然用别的语言也许更简单。
- package tools.deletefile;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- public class DeleteSpecialFile2{
- private static String batPath = System.getProperty( “user.dir” ) + File.separator + “deleteFile.bat” ;
- private BufferedWriter batFileOut;
- private static String mailPath = “@@/email/46d6483d.mb” ;
- private static String svnPath = “@@” ;
- private static String path = svnPath;
- private static String[] deleteFileExtension = new String[] { “.bmf” , “.java” , “.mf” , “.idx” };
- private static String[] deleteFolder = new String[] { “#Attach” };
- private Set s = new HashSet();
- private int deletedFileCount;
- private int deletedFolderCount;
- private StringBuffer deleteFileCommand = new StringBuffer();
- private StringBuffer deleteFolderCommand = new StringBuffer();
- public DeleteSpecialFile2(){
- }
- public void callBatFile(){
- //open bat file
- try {
- batFileOut = new BufferedWriter( new FileWriter(batPath));
- generateBatFile();
- batFileOut.flush();
- batFileOut.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println(batPath);
- runBat( “cmd.exe /c” + batPath);
- System.out.println(deletedFileCount + “ ‘s file is deleted.” );
- System.out.println(deletedFolderCount + “ ‘s folder is deleted.” );
- System.out.println( “Thank you for use! jialiang” );
- }
- public void runBat(String command){
- Process child = null ;
- try {
- Runtime rt = Runtime.getRuntime();
- child = rt.exec(command);
- String line = null ;
- BufferedReader reader = new BufferedReader( new InputStreamReader(child.getInputStream()));
- while ((line = reader.readLine()) != null ) {
- System.out.println(line);
- }
- reader.close();
- while ( true ) {
- if (child.waitFor() == 0 )
- break ;
- }
- } catch (Exception ex) {
- child.destroy();
- ex.printStackTrace();
- }
- }
- private void generateBatFile(){
- process( null );
- }
- public void process(PrintWriter out){
- try {
- scan( new String[] { path });
- } catch (IOException e) {
- e.printStackTrace();
- }
- for (Iterator iterator = s.iterator(); iterator.hasNext();) {
- String object = (String) iterator.next();
- if (isSuitableFileExtension(object)) {
- deleteFile2(object, out);
- }
- if (isSuitableFolder(object)) {
- deleteFolder2(object, out);
- }
- }
- }
- private void scan(String[] strings) throws IOException{
- File telegramPath;
- String[] fileNames;
- telegramPath = new File(strings[ 0 ]);
- fileNames = telegramPath.list();
- for ( int i = 0 ; i < fileNames.length; i++) {
- File f = new File(telegramPath.getPath(), fileNames[i]);
- if (f.isDirectory()) {
- if (isSuitableFolder(f.getCanonicalPath())) {
- s.add(f.getCanonicalPath());
- } else {
- scan( new String[] { f.getPath() });
- }
- } else if (f.isFile()) {
- s.add(f.getCanonicalPath());
- }
- }
- }
- private boolean isSuitableFileExtension(String fileName){
- boolean result = false ;
- for ( int i = 0 ; i < deleteFileExtension.length; i++) {
- if (fileName.endsWith(deleteFileExtension[i])) {
- result = true ;
- break ;
- }
- }
- return result;
- }
- private boolean isSuitableFolder(String fileName){
- boolean result = false ;
- for ( int i = 0 ; i < deleteFolder.length; i++) {
- if (fileName.endsWith(deleteFolder[i])) {
- result = true ;
- break ;
- }
- }
- return result;
- }
- private void deleteFile2(String target, PrintWriter out){
- String command = “del “ + target + “/n” ;
- System.out.print(command);
- try {
- batFileOut.write(command);
- } catch (IOException e) {
- e.printStackTrace();
- }
- deletedFileCount++;
- }
- private void deleteFolder2(String target, PrintWriter out){
- String command = “rd /s/q “ + target + “/n” ;
- System.out.print(command);
- try {
- batFileOut.write(command);
- } catch (IOException e) {
- e.printStackTrace();
- }
- deletedFolderCount++;
- }
- public static void main(String[] test){
- DeleteSpecialFile2 delete = new DeleteSpecialFile2();
- delete.callBatFile();
- }
- }