在JTextArea中显示命令提示符的输出

时间:2017-07-17 16:15:06

标签: java powershell fileinputstream tshark bufferedinputstream

我正在尝试将tshark -i <interface> wireshark实用程序)的cmd输出定向到java程序中的JTextArea。

我尝试了另一种方法,比如存储从提示输出到文本文件,然后使用 FileInputStream / BufferedInputStream 打开程序中的文件。但这不起作用,因为输出是实时的(数据是根据交易界面显示的),我希望数据实时显示在JTextArea中。

因为,文件不能被读取,直到完全写入并关闭程序给我 FileNotFoundException 。代码片段可以让您更好地了解我想要做的事情。

public void controlfunc(int n) {
/*
this is the complete function that takes the serial number of adapter as input,
then runs a command that starts tshark for listening on the adapter 
and saves output to prec file
*/  
String input = JOptionPane.showInputDialog(null, "Please enter the device 
serial number you want to start listening to ?", "Record manager Query",QUESTION_MESSAGE);
int id = Integer.parseInt(input);
    //send id value to a batch file for processing with tshark...
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("powershell.exe TSHARK -i " + id + " >> C:\\Users\\HP\\Documents\\response\\Server_Analyser\\src\\server_analyser\\sreverdump\\prec");
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Error in processing command !\nCheck Software Integrity !", "FATAL ERROR OCCURED !", ERROR_MESSAGE);
        e.printStackTrace();
}
// I need your help here. I cannot open this file and display data in real time. Any other approach is welcomed.
    //get output data from prec to text area output...
    try {
        int ch;
        FileInputStream fin = new FileInputStream("C:\\Users\\HP\\Documents\\response\\Server_Analyser\\src\\server_analyser\\sreverdump\\prec");
        BufferedInputStream bin = new BufferedInputStream(fin);
        while ((ch = bin.read()) != -1) {
            report.append("" + (char) ch);
        }
/*
There is a stop button that sends 0 as argument to the function and the 
code closes the file as no more data is to be read and kills the tshark 
process that was running.
*/ 
        if (n == 0) {
            bin.close();
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("powershell.exe killproc tshark.exe");
        }

    } catch (FileNotFoundException fe) {
        JOptionPane.showMessageDialog(null, "Source file is corrupted or lost !");
    } catch (IOException ex) {
        Logger.getLogger(recordman.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "The program has encountered an IOException !", "record manager Error ", ERROR_MESSAGE);
    }
}



如果夸大其词,我很抱歉。还原谅我的英语。

0 个答案:

没有答案