A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 姚伟涛 中级黑马   /  2012-4-23 22:19  /  2217 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.text;

  2. import java.io.*;
  3. import java.util.Scanner;


  4. public class Foo {

  5.     public static void main(String[] args) throws FileNotFoundException, IOException {
  6.          // TODO code application logic here
  7.          PrintStream ps = null;
  8.          FileInputStream fis = null;
  9.          try
  10.          {
  11.              ps = new PrintStream(new FileOutputStream("test.txt"));
  12.              String[] cmd = {"cmd.exe ","/c","ipconfig","netstat"," > F:\\ababab.txt"};
  13. //          Process p=  Runtime.getRuntime().exec("cmd.exe /c ipconfig > F:\\more.txt");
  14.              Process p=  Runtime.getRuntime().exec(cmd);

  15.         }  
  16.          catch(IOException ex)
  17.          {
  18.              ex.printStackTrace();
  19.          }
  20.          finally
  21.          {
  22.              if(ps != null)
  23.              {
  24.                  ps.close();
  25.              }
  26.          }
  27.      }
  28. }
复制代码
在一个进程里面启动cmd 然后输入ipconfig 和netstat之类的命令 并把输出打印到一个txt里面

5 个回复

倒序浏览
FileInputStream fis = null;

ps = new PrintStream(new FileOutputStream("test.txt"));

一眼就看到这个,你赢了,仔细点啊。。。。。
回复 使用道具 举报
  1. try
  2.          {
  3.              ps = new PrintStream(new FileOutputStream("test.txt"));
  4.              Runtime.getRuntime().exec("cmd.exe /c ipconfig > F:\\ababab.txt"); //这样写就可以了
  5.         }  
复制代码
回复 使用道具 举报
嗯 楼上2位很对啊,但是我觉得开个线程比较好吧。所以我自己做了一个例子。也实现了将ipconfig命令的结果输入到记事本里面。
但是很让人崩溃的是,我的验证语句中BufferedReader读出来的竟然是null,让我很郁闷,调试了n久,打印的readLine字符串总是null。
说是while循环执行条件根本不成立,也就是readLine读出来的是null,郁闷啊。。。。。
大家给我看看,怎么一回事........
上图上代码:
  1. package com.heima;

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

  6. public class UseCmdTest2 {
  7.         public static void main(String[] args) throws IOException, InterruptedException {
  8. //我输出到f盘hell.txt,等会截图你们看看。。但是后面readLine读出来的为什么为null啊,,,,,,,求指教。
  9.         String[] cmd = {"cmd.exe", "/c","ipconfig -all",">F:\\hello.txt"};
  10.                 Process p = Runtime.getRuntime().exec(cmd);
  11.                 System.out.println(p.getInputStream().toString());
  12.                
  13.                 //在网上查到大家都是这么用的。。
  14.                 new Thread(new InputStreamDetection(p.getInputStream())).start();
  15.                 new Thread(new InputStreamDetection(p.getErrorStream())).start();
  16.                 p.getOutputStream().close();
  17.                 int exitValue = p.waitFor();
  18.                 //返回o说明成功了。返回1说明失败!
  19.                 System.out.println("return:"+exitValue);
  20.         }

  21. }

  22. class InputStreamDetection implements Runnable {

  23.         private InputStream is;
  24.        
  25.         public InputStreamDetection(InputStream ins) {
  26.                 this.is = ins;
  27.         }
  28.         @Override
  29.         public void run() {
  30.                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
  31.                 //StringBuffer sb = new StringBuffer();
  32.                 String inputLine = null;
  33.                 try {
  34.                         System.out.println("br.readLine():"+br.readLine());
  35.                         while(null != (inputLine = br.readLine())) {
  36.                                 //sb.append(inputLine).append("\n");
  37.                                 System.out.println("inputLine:"+inputLine);
  38.                         }
  39.                 } catch (IOException e) {
  40.                         e.printStackTrace();
  41.                 }
  42.         }
  43. }
复制代码
myeclipse输出结果:


但是记事本里已经有结果了,图为:
回复 使用道具 举报
本帖最后由 王明(1988) 于 2012-4-24 01:03 编辑

求热心人帮个忙,看看问题在哪里,是什么原因使readLine返回的null。。。。。。。
回复 使用道具 举报
问题解决,O了。{:soso_e128:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马