PrintWriter.close()不调用会泄露资源吗?
深山老宅 2008-04-08 02:52:49 public void send(Socket ss,String str)
{
PrintWriter out=null;
try
{
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(ss.getOutputStream())));
if(str!=null)
{
out.println(str);
out.flush();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
/*finally
{
try{
if(out!=null)
out.close();
}catch(Exception e){}
}*/
}
当我把上面代码中 finally部分写上时,程序编译没错,运行一会后报错:socket closed!
不写finally{}运行正常.
问题:
1.不写会不会泄露资源?不是应该调用close()方法吗?
2.关闭out会导致把socket也关掉吗?