Java调用CMD命令

2023-10-11 14:23:07 浏览数 (1)

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

代码语言:javascript复制
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;  
     }  
	

}

0 人点赞