import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class FindFileTest { private static JButton button;
private static JTextField text;
private static JTextArea ta; public static void main(String[] args) { button = new JButton("运行");
button.addActionListener(new BAction());
text = new JTextField("net user", 30);
ta = new JTextArea(5, 40); ta.setEditable(false); JPanel panel = new JPanel();
JPanel panel1 = new JPanel(); JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel,
panel1);
split.setContinuousLayout(true);
split.setOneTouchExpandable(true); panel.add(text, BorderLayout.NORTH);
panel.add(button, BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createTitledBorder("RunSystem")); panel1.add(new JScrollPane(ta)); panel1.setBorder(BorderFactory.createTitledBorder("SplitPane")); JFrame frame = new JFrame("Button"); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(split, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true); } static class BAction implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
Timer time = new Timer();
time.schedule(new TimeMack(), 1000); } static class TimeMack extends TimerTask { BufferedReader br = null; String s = ""; public void run() { // TODO Auto-generated method stub
try {
String str = text.getText().toLowerCase().toString();
Process p = Runtime.getRuntime().exec(str); InputStream in = p.getInputStream();
br = new BufferedReader(new InputStreamReader(in)); while ((s = br.readLine()) != null) {
ta.setText(s);
System.out.println(s);
s = br.readLine();
} } catch (IOException e) { // TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,
"Please Input aEffective String!!"); } finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} } } }
}

解决方案 »

  1.   

    StringBuffer text=new StringBuffer();
    while ((s = br.readLine()) != null) { System.out.println(text); 
    ta.setText(s); 
    } 如果需要换行在text.append(s);后面做处理
      

  2.   

    StringBuffer text=new StringBuffer();
    while ((s = br.readLine()) != null) { 
    text.append(s);
    System.out.println(text); 
    ta.setText(s); 

      

  3.   

    将读取的内容放到一个容器中,比如stringbuffer,或者集合list,然后一起输出就可以了
      

  4.   

    楼主这个地方出错了:                static class TimeMack extends TimerTask { BufferedReader br = null; String s = ""; public void run() { // TODO Auto-generated method stub
    try {
    String str = text.getText().toLowerCase().toString();
    Process p = Runtime.getRuntime().exec(str); InputStream in = p.getInputStream();
    br = new BufferedReader(new InputStreamReader(in)); String temp = new String("");

    while ((s = br.readLine()) != null) {
    // ta.setText(s); // ta中的数据每次循环都发生变化,在最后一次循环之后,s是一个空值,所以在最后什么都没输出
    temp += s + "\n";
    System.out.println(s);
    // s = br.readLine();      // 楼主之前在这里多读了一行。
    }
    ta.setText(temp); } catch (IOException e) { // TODO Auto-generated catch block
    JOptionPane.showMessageDialog(null, "Please Input a Effective String!!"); } finally {
    try {
    if (br != null) {
    br.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    } } }
    输出结果(输入netuser):
    Administrator            ASPNET                   Guest                    
    HelpAssistant            IUSR_43DB0D5296634EC     IWAM_43DB0D5296634EC     
    SUPPORT_388945a0         
    命令成功完成。