Reputation: 33
This is my code:
Process p=Runtime.getRuntime().exec("something command");
String s;
JFrame frame = new JFrame();
frame.setSize(600, 400);
JTextField A = new JTextField();
A.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String s = A.getText();
System.out.println("I send a text: " + s);
try{
p.getOutputStream().write(s.getBytes());
p.getOutputStream().close();
}catch(Exception ex){
ex.printStackTrace();
}
A.setText("");
}
});
frame.add(A);
frame.setVisible(true);
// Read command standard input
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
I want to use p.getOutputStream() to send the message two times, but the problem is I need to close the OutputStream to send. I can't send again because it have been closd. Can I reconnect the OutputStream or no need to close the OutputStream?
Thx:)
Upvotes: 0
Views: 540
Reputation: 201447
Instead of calling close()
, you can call flush()
which flushes this output stream and forces any buffered output bytes to be written out. Then you can close()
, after you've written your messages the desired number of times.
Upvotes: 1