我正在尝试使用BufferedWriter
从客户端或服务器到客户端向服务器发送消息。但是什么都不会发送,但是如果您向它发送消息,它会收听。我不确定我做错了什么,但我认为问题出在此处。
ActionListener buttonActive= new ActionListener(){
public void actionPerformed(ActionEvent e) {
messageTextArea.append(textMessage.getText()+ "\n");
sendText = textMessage.getText();
}
};
sendButton.addActionListener(buttonActive);
private void startSending(){
SwingWorker <Void, Void> sendingThread = new SwingWorker <Void, Void>(){
protected Void doInBackground() throws Exception {
BufferedWriter writer = null;
writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
while(connected){
writer.write(sendText);
writer.flush();
}
return null;
}
};
sendingThread.execute();
}
@trashgod 和 @madProgrammer 给了我一个使用 printWriter 的解决方案,但我希望我使用BufferedWriter
+和+制作更多版本BufferedReader
,但是我想制作+ 。OutputStream+ InputStream
BufferedWriter
PrintWriter
BufferedWriter
BufferedReader