Chinaunix首页 | 论坛 | 博客
  • 博客访问: 445426
  • 博文数量: 80
  • 博客积分: 2301
  • 博客等级: 大尉
  • 技术积分: 884
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-16 20:07
个人简介

I\'m interested in mathematics and Daoism. Welcome to talk about these subjects with me.

文章分类

全部博文(80)

文章存档

2017年(2)

2016年(16)

2015年(4)

2014年(6)

2013年(22)

2012年(2)

2011年(1)

2010年(4)

2009年(20)

2008年(2)

2007年(1)

我的朋友

分类: Java

2016-10-12 13:19:15


点击(此处)折叠或打开

  1. package Runtime;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;

  6. public class OutputTest {

  7.     //获取子进程的标准输出
  8.     public static void main(String[] args) {
  9.         System.out.println("------------------test windows commands --------------------");
  10.         Runtime runtime = Runtime.getRuntime();
  11.         
  12.         String command = "ipconfig";
  13.         Process process = null;
  14.         try {
  15.             process = runtime.exec(command);
  16.         } catch (IOException e) {
  17.             // TODO Auto-generated catch block
  18.             e.printStackTrace();
  19.         }
  20.         
  21.         String charsetName = "GB2312";
  22.         //charsetName = "utf-8";
  23.         InputStreamReader reader;
  24.         try {
  25.             reader = new InputStreamReader(process.getInputStream(), charsetName);
  26.         } catch (UnsupportedEncodingException e1) {
  27.             // TODO Auto-generated catch block
  28.             e1.printStackTrace();
  29.             return ;
  30.         }
  31.         BufferedReader bufferedReader = new BufferedReader(reader);
  32.         
  33.         String line;
  34.         try {
  35.             
  36.             //System.out.println(bufferedReader);
  37.             while(true) {
  38.                 line = bufferedReader.readLine();
  39.                 if (line == null)
  40.                     break;
  41.                 System.out.println(line);
  42.             }
  43.             process.waitFor();
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         } catch (InterruptedException e) {
  47.             e.printStackTrace();
  48.         }    
  49.     }//main ends
  50. }

阅读(2271) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~