java cmd 等待输入_JAVA:调用cmd指令(支持多次手工输入)
1 packagecom.le.tool;2
3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.IOException;6 importjava.io.InputStream;7 importjava.io.InputStreamReader;8 importjava.io.PrintWriter;9 importjava.nio.charset.Charset;10
11 /**
12 * java調(diào)用cmd指令工具類13 *14 *@authorle.li15 *16 */
17 public classExecuteUtil {18 /**
19 * 避免亂碼,如果沒(méi)有傳入語(yǔ)言編號(hào),默認(rèn)使用英文437
20 * D:\>chcp /? 顯示或設(shè)置活動(dòng)代碼頁(yè)編號(hào)。
21 * CHCP [nnn]
22 * nnn 指定代碼頁(yè)編號(hào)。
23 * 不帶參數(shù)鍵入 CHCP 以顯示活動(dòng)代碼頁(yè)編號(hào)。
24 */
25 private static final String DEFAULT_LANGUAGE_CODE = "437";26
27 /**
28 * window系統(tǒng)默認(rèn)語(yǔ)言:GBK29 */
30 private static final String DEFAULT_LANGUAGE = "GBK";31
32 public static voidmain(String[] args) {33 //executeLink();34
35 //executeCmd("dir .");36
37 //舉例直接把bat文件當(dāng)cmd指令調(diào)用
38 String cmd = null;39 String fileName = "test.bat";40 File f = new File(".");41 try{42 cmd = f.getCanonicalPath() + File.separator +fileName;43 } catch(IOException e) {44 //e.printStackTrace();
45 System.err.println("get cmd file error.");46 }47 executeCmd(cmd);48 }49
50 /**
51 * 獲取操作系統(tǒng)默認(rèn)語(yǔ)言52 *53 *@returnString54 *@seejava虛擬機(jī)啟動(dòng)默認(rèn)的編碼(一般和java文件設(shè)置格式一致)
55 * System.out.println(Charset.defaultCharset());
56 * 查看預(yù)置的變量信息:System.getProperties().list(System.out);
57 * 屬性:
58 * 文件編碼:file.encoding
59 * 系統(tǒng)默認(rèn)編碼sun.jnu.encoding60 */
61 private staticString getsystemLanguage() {62 return null == System.getProperty("sun.jnu.encoding") ?DEFAULT_LANGUAGE63 : System.getProperty("sun.jnu.encoding");64 }65
66 /**
67 * 執(zhí)行cmd指令68 *@paramcmd 執(zhí)行指令69 */
70 public static voidexecuteCmd(String cmd) {71 executeLink(DEFAULT_LANGUAGE_CODE, true, cmd);72 }73
74 /**
75 * cmd手工輸入交互處理窗口76 */
77 public static voidexecuteLink() {78 executeLink(DEFAULT_LANGUAGE_CODE, false, "");79 }80
81 /**
82 * cmd交互處理窗口83 *84 *@paramlanguageCode 系統(tǒng)語(yǔ)言編碼85 *@paramisOneRun 只執(zhí)行cmd指令86 *@paramcmd 執(zhí)行的指令87 *@see在中文windows系統(tǒng)中,根據(jù)編碼需要設(shè)置編碼 chcp 65001 就是換成UTF-8代碼頁(yè)
88 * chcp 936 可以換回默認(rèn)的GBK
89 * chcp 437 是美國(guó)英語(yǔ)
90 */
91 public static void executeLink(String languageCode, booleanisOneRun, String cmd) {92 try{93 String cmdBin = "cmd";94 if(isOneRun) {95 cmdBin = "cmd /c ";96 }97 Process process = Runtime.getRuntime().exec(cmdBin +cmd);98 PrintWriter writer = newPrintWriter(process.getOutputStream());99 if (!isOneRun) {100 //此處可以預(yù)置交互指令101 //writer.println("chcp " + languageCode);
102 writer.println("echo Hello World.");103 writer.flush();104 }105 CommandThread commandThread = newCommandThread(writer);106 commandThread.setName("ExecuteCmdThread");107 commandThread.start();108 ProcessInputStreamThread inputThread = newProcessInputStreamThread(process.getInputStream());109 ProcessInputStreamThread errorThread = newProcessInputStreamThread(process.getErrorStream());110 inputThread.setName("InputStreamThread");111 inputThread.start();112 errorThread.setName("ErrorStreamThread");113 errorThread.start();114 //即使添加下邊的一句也不會(huì)使線程結(jié)束115 //Thread.currentThread().interrupt();
116 } catch(Exception e) {117 e.printStackTrace();118 }119 }120
121 static class CommandThread extendsThread {122 PrintWriter writer;123 BufferedReader br = null;124
125 CommandThread(PrintWriter writer) {126 this.writer =writer;127 //避免出現(xiàn)亂碼問(wèn)題,直接使用系統(tǒng)默認(rèn)的編碼格式
128 br = new BufferedReader(newInputStreamReader(System.in, Charset.forName(getsystemLanguage())));129 this.setDaemon(true);130 }131
132 @Override133 public voidrun() {134 try{135 String cmd = null;136 while ((cmd = br.readLine()) != null) {137 writer.println(cmd);138 writer.flush();139 }140 } catch(IOException e) {141 e.printStackTrace();142 } finally{143 if (null !=writer) {144 writer.close();145 }146 if (null !=br) {147 try{148 br.close();149 } catch(IOException e) {150 //TODO Auto-generated catch block
151 e.printStackTrace();152 }153 }154 }155 }156 }157
158 static class ProcessInputStreamThread extendsThread {159
160 InputStream input;161 BufferedReader breader = null;162
163 ProcessInputStreamThread(InputStream input) {164 this.input =input;165 //避免出現(xiàn)亂碼問(wèn)題,直接使用系統(tǒng)默認(rèn)的編碼格式
166 breader = new BufferedReader(newInputStreamReader(input, Charset.forName(getsystemLanguage())));167 }168
169 @Override170 public voidrun() {171 try{172 String str = null;173 while ((str = breader.readLine()) != null) {174 System.out.println(str);175 }176 } catch(IOException e) {177 e.printStackTrace();178 } finally{179 if (null !=input) {180 try{181 input.close();182 } catch(IOException e) {183 //TODO Auto-generated catch block
184 e.printStackTrace();185 }186 }187 if (null !=breader) {188 try{189 breader.close();190 } catch(IOException e) {191 //TODO Auto-generated catch block
192 e.printStackTrace();193 }194 }195 }196 }197 }198 }
總結(jié)
以上是生活随笔為你收集整理的java cmd 等待输入_JAVA:调用cmd指令(支持多次手工输入)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql mongodb qps_极高
- 下一篇: 太阳能光伏发电接错电能不能电死人