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()来调用这个文件就解决问题了。
源代码如下:
有一些冗余的东西没有删除。
当然用别的语言也许更简单。
刚开始我的思路是用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();
- }
- }
- Java中调用DOS命令的问题
- java 在window 下调用dos 命令的问题。
- Java中如何调用DOS命令
- Java调用DOS命令
- JAVA调用DOS命令
- java调用dos命令
- java调用Dos命令
- Java调用dos命令
- java调用dos命令
- Java调用DOS命令
- java调用dos命令
- java 调用DOS命令
- java调用dos命令
- Java 调用dos命令
- Delphi中对Dos命令的调用
- Java调用windows Dos命令的用法
- python中调用dos命令
- JAVA如何调用DOS命令
- 纯CSS的下拉菜单 支持IE6 IE7 Firefox
- 使用Command模式实现撤销机制[1]
- “automation服务器不能创建对象”的问题的解决方案大全
- struts中应用MultipartRequest
- 这是我对IS-IS的一些见解
- Java中调用DOS命令的问题
- JS函数集合大全(转载)
- 不睡觉+可乐+洗发水+高露洁=缩短生命
- SQL Server 2005 中的商务智能和数据仓库(1)
- 移植google操作系统android的5个步骤之(一)
- 解决extjs在firefox中关闭窗口再打开后iframe中js函数访问不到的问题
- 报表服务问题的处理
- SQLServer : EXEC和sp_executesql的区别
- ECMAScript基础(引用类型)