Java中调用DOS命令的问题

最近遇到一个问题,就是我的mail文件夹下面的mail太多了,使得Becky的浏览速度变得很慢,所以必须先备份原先的数据,但是又不能将目录结构删除,所以我想写一个java程序来完成这个工作。

刚开始我的思路是用java中出需要删除的文件或文件夹,保存在一个set中,然后对这个set进行遍历,每一项拼成一个DOS命令,然后用Runtime.getRuntime().exec()来运行,思路是对的,但是谁知道当要删除的文件很多的时候,Runtime.getRuntime().exec()产生的进程就会挂起,一直没有响应,我很郁闷,不知道是为什么,上网找资料也没有找到,所以另辟蹊径了。
将所有的命令输出到一个bat文件中,然后用Runtime.getRuntime().exec()来调用这个文件就解决问题了。
源代码如下:
有一些冗余的东西没有删除。
当然用别的语言也许更简单。

  1. package tools.deletefile;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import java.io.PrintWriter;
  10. import java.util.HashSet;
  11. import java.util.Iterator;
  12. import java.util.Set;
  13. public class DeleteSpecialFile2{
  14. private static String batPath = System.getProperty( “user.dir” ) + File.separator + “deleteFile.bat” ;
  15. private BufferedWriter batFileOut;
    1. private static String mailPath = “@@/email/46d6483d.mb” ;
  16. private static String svnPath = “@@” ;
  17. private static String path = svnPath;
    1. private static String[] deleteFileExtension = new String[] { “.bmf” , “.java” , “.mf” , “.idx” };
  18. private static String[] deleteFolder = new String[] { “#Attach” };
    1. private Set s = new HashSet();
  19. private int deletedFileCount;
  20. private int deletedFolderCount;
    1. private StringBuffer deleteFileCommand = new StringBuffer();
  21. private StringBuffer deleteFolderCommand = new StringBuffer();
    1. public DeleteSpecialFile2(){
  22. }
    1. public void callBatFile(){
  23. //open bat file
  24. try {
  25. batFileOut = new BufferedWriter( new FileWriter(batPath));
  26. generateBatFile();
  27. batFileOut.flush();
  28. batFileOut.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
    1. System.out.println(batPath);
  32. runBat( “cmd.exe /c” + batPath);
  33. System.out.println(deletedFileCount + “ ‘s file is deleted.” );
  34. System.out.println(deletedFolderCount + “ ‘s folder is deleted.” );
  35. System.out.println( “Thank you for use! jialiang” );
  36. }
    1. public void runBat(String command){
  37. Process child = null ;
  38. try {
  39. Runtime rt = Runtime.getRuntime();
  40. child = rt.exec(command);
  41. String line = null ;
  42. BufferedReader reader = new BufferedReader( new InputStreamReader(child.getInputStream()));
  43. while ((line = reader.readLine()) != null ) {
  44. System.out.println(line);
  45. }
  46. reader.close();
  47. while ( true ) {
  48. if (child.waitFor() == 0 )
  49. break ;
  50. }
  51. } catch (Exception ex) {
  52. child.destroy();
  53. ex.printStackTrace();
  54. }
  55. }
    1. private void generateBatFile(){
  56. process( null );
  57. }
    1. public void process(PrintWriter out){
  58. try {
  59. scan( new String[] { path });
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. for (Iterator iterator = s.iterator(); iterator.hasNext();) {
  64. String object = (String) iterator.next();
  65. if (isSuitableFileExtension(object)) {
  66. deleteFile2(object, out);
  67. }
  68. if (isSuitableFolder(object)) {
  69. deleteFolder2(object, out);
  70. }
  71. }
  72. }
    1. private void scan(String[] strings) throws IOException{
  73. File telegramPath;
  74. String[] fileNames;
  75. telegramPath = new File(strings[ 0 ]);
  76. fileNames = telegramPath.list();
  77. for ( int i = 0 ; i < fileNames.length; i++) {
  78. File f = new File(telegramPath.getPath(), fileNames[i]);
  79. if (f.isDirectory()) {
  80. if (isSuitableFolder(f.getCanonicalPath())) {
  81. s.add(f.getCanonicalPath());
  82. } else {
  83. scan( new String[] { f.getPath() });
  84. }
  85. } else if (f.isFile()) {
  86. s.add(f.getCanonicalPath());
  87. }
  88. }
  89. }
    1. private boolean isSuitableFileExtension(String fileName){
  90. boolean result = false ;
  91. for ( int i = 0 ; i < deleteFileExtension.length; i++) {
  92. if (fileName.endsWith(deleteFileExtension[i])) {
  93. result = true ;
  94. break ;
  95. }
  96. }
  97. return result;
  98. }
    1. private boolean isSuitableFolder(String fileName){
  99. boolean result = false ;
  100. for ( int i = 0 ; i < deleteFolder.length; i++) {
  101. if (fileName.endsWith(deleteFolder[i])) {
  102. result = true ;
  103. break ;
  104. }
  105. }
  106. return result;
  107. }
    1. private void deleteFile2(String target, PrintWriter out){
  108. String command = “del “ + target + “/n” ;
  109. System.out.print(command);
  110. try {
  111. batFileOut.write(command);
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. }
  115. deletedFileCount++;
  116. }
    1. private void deleteFolder2(String target, PrintWriter out){
  117. String command = “rd /s/q “ + target + “/n” ;
  118. System.out.print(command);
  119. try {
  120. batFileOut.write(command);
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. deletedFolderCount++;
  125. }
    1. public static void main(String[] test){
  126. DeleteSpecialFile2 delete = new DeleteSpecialFile2();
  127. delete.callBatFile();
  128. }
  129. }