小编典典

Java-了解PrintWriter和冲洗需求

java

好的,首先我为所有代码道歉,但是我觉得太多的代码总比不够好。我正在努力做一个简单的聊天客户端和印刷作家。使用现在的代码,它将与服务器类进行交互,并且可以很好地进行打印并打印我想要打印的内容。但是,当我删除’writer.flush();’时
它将停止打印。以我的理解-这显然是错误的-‘writer.println(outgoing.getText());’
应该就足够了,因为这会发送试图发送到服务器的文本。我知道冲洗会强制所有信息写入,但是当我已经写了我想写的东西时为什么要这样做?

public class SimpleChatClientA {

    JTextArea incoming;
    JTextField outgoing;
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;

    public void go(){

        JFrame frame = new JFrame("Ludicrously Simple Chat Client");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel mainPanel = new JPanel();

        incoming = new JTextArea(15,50);
        incoming.setLineWrap(true);
        incoming.setWrapStyleWord(true);
        incoming.setEditable(false);

        JScrollPane qScroller = new JScrollPane(incoming);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        outgoing = new JTextField(20);

        JButton sendButton = new JButton("Send");
        sendButton.addActionListener(new SendButtonListener());

        mainPanel.add(qScroller);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);
        setUpNetworking();

        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();

        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        frame.setSize(400, 500);
        frame.setVisible(true);


    }//close go

    public void setUpNetworking(){
        try{

            sock = new Socket("127.0.0.1", 5000);

            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());

            reader = new BufferedReader(streamReader);
            writer = new PrintWriter(sock.getOutputStream());
            System.out.println("network established");
        }catch(Exception e){
            e.printStackTrace();
        }
    }//close setupnetworking

    public class SendButtonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {

            try{
                writer.println(outgoing.getText());
                writer.flush();
            }catch(Exception ex){
                ex.printStackTrace();
            }
            outgoing.setText("");
            outgoing.requestFocus();
        }

    }//close sendbuttonlistener inner class


    public class IncomingReader implements Runnable{

        public void run(){
            String message;
            try{

                while((message = reader.readLine()) != null){
                    System.out.println("read " + message);
                    incoming.append(message + "\n");
                }

            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }//close inner class incomingreader


    public static void main(String[] args){
        new SimpleChatClientA().go();
    }


}

在此先感谢您,如果我能提供更多信息,请让我知道我第一次正确发布到stackoverflow。


阅读 213

收藏
2020-11-23

共1个答案

小编典典

仅当启用属性println()时,PrintWriter类的方法才将数据“提交”到流中autoFlush

查看Java文档中的以下引用:

如果启用了自动刷新,则仅在调用println,printf或format方法之一时才执行

链接到Java文档:https
:
//docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter(
java.io.OutputStream,%
20boolean)

如果需要将消息发送到服务器,请PrintWriter在启用“自动刷新”选项的情况下创建对象。

因此,在创建writer对象的地方,只需执行以下操作:

writer = new PrintWriter(sock.getOutputStream(), true);

那应该做。希望能帮助到你!

2020-11-23