1. 程式人生 > >Java執行cmd命令並接收返回結果

Java執行cmd命令並接收返回結果

	String command="ipconfig -all";
		String s="IPv4";
		String line = null;
		StringBuilder sb = new StringBuilder();
		Runtime runtime = Runtime.getRuntime();
		try {
		Process process = runtime.exec(command);
		BufferedReader	bufferedReader = new BufferedReader
				(new InputStreamReader(process.getInputStream()));
		
		
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line + "\n");
				if (line.contains(s)) {
					System.out.println(line);
				}
			}
		} catch (IOException e) {
			// TODO 自動生成的 catch 塊
			e.printStackTrace();
		}
		//System.out.println(sb.toString());