Java调用CMD命令

Windows下复制:
copy C:\server\data\ccgavr\1.png C:\server\data\ccgavr\2.png
Windows重命名:
ren 1.png 3.png
ren C:\server\data\ccgavr\1.png C:\server\data\ccgavr\3.png(DOS中提示命令语法不正确,powershell则可以)

package com.pgz.cmd;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import com.pgz.util.LogUtil;

public class CMDTest {

	public static void main(String[] args) {
		String cmdStr = "mspaint";// 打开画板
		cmdStr = "ren C:/server/data/ccgavr/3.png C:/server/data/ccgavr/4.png";// 重命名,DOS中不可
		cmdStr = "ren 3.png 4.png";
		cmdStr = "ipconfig";
//		executeCMD(cmdStr);
		System.out.println(executeLocalCmd(cmdStr, null));
	}

	/**
	 * 直接System.out
	 * 测试OK
	 * @param cmdStr  CMD命令字符
	 */
	public static void executeCMD(String cmdStr) {

		Runtime run = Runtime.getRuntime();
		try {
//			Process process = run.exec("cmd.exe /k start " + cmdStr);
			Process process = run.exec("cmd.exe /c " + cmdStr);
			InputStream in = process.getInputStream();
			while (in.read() != -1) {
				System.out.println(in.read());
			}
			in.close();
			process.waitFor();
		} catch (Exception e) {
			LogUtil.exception(e);
		}

	}

	/**
	 * 将日志输出到文件
	 * 待测试
	 * @param cmmands    命令数组adb logcat -v time > d:/adb.log { "adb",
	 *                   "logcat","-v","time"}
	 * @param logToFile  保存的文件
	 * @param dirTodoCMD 在此目录下执行
	 * @return
	 */
	public static String executeCMDFile(String[] cmmands, String logToFile, String dirTodoCMD) {

		try {
			ProcessBuilder builder = new ProcessBuilder(cmmands);
			if (dirTodoCMD != null)
				builder.directory(new File(dirTodoCMD));
			builder.redirectErrorStream(true);
			builder.redirectOutput(new File(logToFile));
			Process process = builder.start();
			process.waitFor();
			// 得到命令执行后的结果
			InputStream is = process.getInputStream();
			BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "GBK"));
			String line = null;
			StringBuffer sbBuffer = new StringBuffer();
			while ((line = buffer.readLine()) != null) {
				sbBuffer.append(line);
			}

			is.close();
			return sbBuffer.toString();
		} catch (Exception e) {
			LogUtil.exception(e);
		}
		return null;
	}
	
	/**
	 * Windows执行本地命令行
	 * 测试ok
	 * @param cmd
	 * @param workpath  在此目录下执行
	 * @return
	 */
	 public static String executeLocalCmd(String cmd, File workpath) {  
		 try {  
			 String[] cmdA = { "cmd.exe", "/c", cmd };  
             Process process = null;
             if(workpath==null){
            	 process = Runtime.getRuntime().exec(cmdA);  
             }else{
            	 process = Runtime.getRuntime().exec(cmdA, null, workpath);  
             }
//             LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
             LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream(),"GBK"));
             StringBuffer sb = new StringBuffer();  
             String line;  
             while ((line = br.readLine()) != null) {  
            	 sb.append(line).append("\n");  
             }  
             return sb.toString();  
         } catch (Exception e) {
        	 LogUtil.exception(e);
         }  
         return null;  
     }  
	

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JaneYork

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值