﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-mooninwell</title><link>http://www.blogjava.net/mooninwell/</link><description /><language>zh-cn</language><lastBuildDate>Wed, 29 Apr 2026 01:32:43 GMT</lastBuildDate><pubDate>Wed, 29 Apr 2026 01:32:43 GMT</pubDate><ttl>60</ttl><item><title>经典的IO代码为什么有资源泄漏？</title><link>http://www.blogjava.net/mooninwell/archive/2006/03/24/37277.html</link><dc:creator>mooninwell</dc:creator><author>mooninwell</author><pubDate>Fri, 24 Mar 2006 13:47:00 GMT</pubDate><guid>http://www.blogjava.net/mooninwell/archive/2006/03/24/37277.html</guid><wfw:comment>http://www.blogjava.net/mooninwell/comments/37277.html</wfw:comment><comments>http://www.blogjava.net/mooninwell/archive/2006/03/24/37277.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.blogjava.net/mooninwell/comments/commentRss/37277.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mooninwell/services/trackbacks/37277.html</trackback:ping><description><![CDATA[
		<p>最近在开发中，使用到IO操作进行反序列化操作，使用了在各种资料中常见的关于IO操作的经典代码，在自测和测试人员的测试中都没有发现任何问题！<br />代码如下</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> Object readObject(File file)<br />    {<br />        Object o </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">;<br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (file.exists())<br />        {<br />            ObjectInputStream ois </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">;<br />            </span>
				<span style="COLOR: #0000ff">try</span>
				<span style="COLOR: #000000">
						<br />            {<br />                ois </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> ObjectInputStream(</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> FileInputStream(file));<br />                o </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> ois.readObject();<br />            }<br />            </span>
				<span style="COLOR: #0000ff">catch</span>
				<span style="COLOR: #000000"> (Throwable e)<br />            {<br />                e.printStackTrace();<br />            }<br />            </span>
				<span style="COLOR: #0000ff">finally</span>
				<span style="COLOR: #000000">
						<br />            {<br />                </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (ois </span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">)<br />                {<br />                    </span>
				<span style="COLOR: #0000ff">try</span>
				<span style="COLOR: #000000">
						<br />                    {<br />                        ois.close();<br />                    }<br />                    </span>
				<span style="COLOR: #0000ff">catch</span>
				<span style="COLOR: #000000"> (IOException e)<br />                    {<br />                        e.printStackTrace();<br />                    }<br />                }<br />            }            <br />        }<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> o;<br />    }</span>
		</div>
		<br />忽然一天测试人员联系我说这部分代码导致系统无法启动，原因仅仅是因为让该部分代码不断的处理一个为序列化文件！<br />我仔细分析一下代码，没有问题呀，反序列化失败就直接关闭了ois对象，不可能造成资源泄漏，于是用单步跟踪的方式<br />查看处理非法文件的详细情况：<br />1、创建FileInputStream 没有问题<br />2、创建ObjectInputStream失败 没有问题<br />3、进入catch块，打印堆栈 没有问题<br />4、进入finnaly块，if (ois != null)为false，这时意识到出问题了<br />原来创建FileInputStream成功，此时已经申请了资源，但在后续创建ObjectInputStream时失败，但此时又没有保存FileInputStream<br />的引用，无法释放资源，最终导致了资源泄漏。看来在以后的<font color="#0000ff"><strong>IO的编程中一定要保存基本流的引用</strong></font>，否则在类似上面的情况<br />在转换流失败后，无法安全的释放资源！<br />下面为修改后的代码：<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object readObject(File file)<br />    {<br />        Object o </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (file.exists())<br />        {<br />            FileInputStream fis </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br />            ObjectInputStream ois </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br />            </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"><br />            {<br />                fis </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> FileInputStream(file);<br />                ois </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ObjectInputStream(fis);<br />                o </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ois.readObject();<br />            }<br />            </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (Throwable e)<br />            {<br />                e.printStackTrace();<br />            }<br />            </span><span style="COLOR: #0000ff">finally</span><span style="COLOR: #000000"><br />            {<br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (fis </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)<br />                {<br />                    </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"><br />                    {<br />                        fis.close();<br />                    }<br />                    </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException e)<br />                    {<br />                        e.printStackTrace();<br />                    }<br />                }<br />            }            <br />        }<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> o;<br />    }</span></div><br /><br /><img src ="http://www.blogjava.net/mooninwell/aggbug/37277.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mooninwell/" target="_blank">mooninwell</a> 2006-03-24 21:47 <a href="http://www.blogjava.net/mooninwell/archive/2006/03/24/37277.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>