Java流是否应关闭在支持套接字关闭之前? [英] Should Java streams be closed Before the backing socket is closed?

查看:239
本文介绍了Java流是否应关闭在支持套接字关闭之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public void disconnect()
{
    running = false;
    if(thread != null)
        thread.interrupt();
    thread = null;

    try
    {
        socket.close();
    }catch(Exception e){e.printStackTrace();}
}



<我有一个BufferedReader来收集来自流的输入和一个通过输出流发送消息的PrintWriter。在关闭套接字之前,BufferedReader和PrintWriter应该关闭吗?让i / o流保持打开是不是很糟糕?

I have a BufferedReader that collects the input from the stream and a PrintWriter that sends messages through the output stream. Should the BufferedReader and the PrintWriter be closed before i close the socket? Is it bad to leave the i/o streams open?

推荐答案

根据 Java Networking Tutorial


你应该在关闭套接字本身之前关闭连接到套接字的所有流。

"You should close any streams connected to a socket before you close the socket itself."

这篇关于Java流是否应关闭在支持套接字关闭之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆