java程序,如何使用Outputstream发送两次消息
这是我的代码:
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);
}
我想使用p.getOutputStream()发送两次消息,但问题是我需要关闭OutputStream才能发送。我无法再次发送,因为它已被关闭。我可以重新连接OutputStream,还是不需要关闭OutputStream
Thx:)
# 1 楼答案
您可以调用^{} ,而不是调用^{} ,它刷新这个输出流,并强制写出任何缓冲的输出字节然后你可以
close()
,在你将信息写入所需的次数之后