Java中调用DOS命令的问题

来源:互联网 发布:网络渗透工程师认证 编辑:程序博客网 时间:2024/05/28 16:05
 最近遇到一个问题,就是我的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;
  16.     
  17.     private static String   mailPath            = "@@/email/46d6483d.mb";
  18.     private static String   svnPath             = "@@";
  19.     private static String   path                = svnPath;
  20.     
  21.     private static String[] deleteFileExtension = new String[] { ".bmf"".java"".mf"".idx"};
  22.     private static String[] deleteFolder        = new String[] { "#Attach" };
  23.     
  24.     private Set s = new HashSet();
  25.     private int             deletedFileCount;
  26.     private int             deletedFolderCount;
  27.     
  28.     private StringBuffer deleteFileCommand = new StringBuffer();
  29.     private StringBuffer deleteFolderCommand = new StringBuffer();
  30.     public DeleteSpecialFile2(){
  31.     }
  32.     
  33.     public void callBatFile(){
  34.         //open bat file
  35.         try {
  36.             batFileOut = new BufferedWriter(new FileWriter(batPath));
  37.             generateBatFile();
  38.             batFileOut.flush();
  39.             batFileOut.close();
  40.         } catch (IOException e) {
  41.             e.printStackTrace();
  42.         }
  43.         
  44.         System.out.println(batPath);
  45.         runBat("cmd.exe /c" + batPath);
  46.         System.out.println(deletedFileCount + " 's file is deleted.");
  47.         System.out.println(deletedFolderCount + " 's folder is deleted.");
  48.         System.out.println("Thank you for use! jialiang");
  49.     }
  50.     public void runBat(String command){
  51.         Process child = null;
  52.         try {
  53.             Runtime rt = Runtime.getRuntime();
  54.             child = rt.exec(command);
  55.             String line = null;
  56.             BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
  57.             while ((line = reader.readLine()) != null) {
  58.                 System.out.println(line);
  59.             }
  60.             reader.close();
  61.             while (true) {
  62.                 if (child.waitFor() == 0)
  63.                     break;
  64.             }
  65.         } catch (Exception ex) {
  66.             child.destroy();
  67.             ex.printStackTrace();
  68.         }
  69.     } 
  70.     
  71.     private void generateBatFile(){
  72.         process(null);
  73.     }
  74.  
  75.     public void process(PrintWriter out){
  76.         try {
  77.             scan(new String[] { path });
  78.         } catch (IOException e) {
  79.             e.printStackTrace();
  80.         }
  81.         for (Iterator iterator = s.iterator(); iterator.hasNext();) {
  82.             String object = (String) iterator.next();
  83.             if (isSuitableFileExtension(object)) {
  84.                 deleteFile2(object, out);
  85.             }
  86.             if (isSuitableFolder(object)) {
  87.                 deleteFolder2(object, out);
  88.             }
  89.         }
  90.     }
  91.     private void scan(String[] strings) throws IOException{
  92.         File telegramPath;
  93.         String[] fileNames;
  94.         telegramPath = new File(strings[0]);
  95.         fileNames = telegramPath.list();
  96.         for (int i = 0; i < fileNames.length; i++) {
  97.             File f = new File(telegramPath.getPath(), fileNames[i]);
  98.             if (f.isDirectory()) {
  99.                 if (isSuitableFolder(f.getCanonicalPath())) {
  100.                     s.add(f.getCanonicalPath());
  101.                 } else {
  102.                     scan(new String[] { f.getPath() });
  103.                 }
  104.             } else if (f.isFile()) {
  105.                 s.add(f.getCanonicalPath());
  106.             }
  107.         }
  108.     }
  109.     private boolean isSuitableFileExtension(String fileName){
  110.         boolean result = false;
  111.         for (int i = 0; i < deleteFileExtension.length; i++) {
  112.             if (fileName.endsWith(deleteFileExtension[i])) {
  113.                 result = true;
  114.                 break;
  115.             }
  116.         }
  117.         return result;
  118.     }
  119.     private boolean isSuitableFolder(String fileName){
  120.         boolean result = false;
  121.         for (int i = 0; i < deleteFolder.length; i++) {
  122.             if (fileName.endsWith(deleteFolder[i])) {
  123.                 result = true;
  124.                 break;
  125.             }
  126.         }
  127.         return result;
  128.     }
  129.     
  130.     private void deleteFile2(String target, PrintWriter out){
  131.         String command = "del " + target + "/n";
  132.         System.out.print(command);
  133.         try {
  134.             batFileOut.write(command);
  135.         } catch (IOException e) {
  136.             e.printStackTrace();
  137.         }
  138.         deletedFileCount++;
  139.     }
  140.     private void deleteFolder2(String target, PrintWriter out){
  141.         String command = "rd /s/q " + target + "/n";
  142.         System.out.print(command);
  143.         try {
  144.             batFileOut.write(command);
  145.         } catch (IOException e) {
  146.             e.printStackTrace();
  147.         }
  148.         deletedFolderCount++;
  149.     }
  150.     public static void main(String[] test){
  151.         DeleteSpecialFile2 delete = new DeleteSpecialFile2();
  152.         delete.callBatFile();
  153.     }
  154. }

原创粉丝点击