這篇文章給大家分享的是有關(guān)inputstreamreader的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

inputstreamreader程序創(chuàng)建或分配資源后,未進(jìn)行合理釋放,將會(huì)產(chǎn)生不恰當(dāng)?shù)馁Y源釋放缺陷。在繁忙的程序環(huán)境下,可能導(dǎo)致Java虛擬機(jī)不能有效的使用I/O對(duì)象。
inputstreamreader缺陷樣本
在下面Java方法中,創(chuàng)建I/O流對(duì)象后未進(jìn)行合理釋放,程序依靠Java虛擬機(jī)的垃圾回收機(jī)制釋放I/O流資源,事實(shí)上,程序不能確定何時(shí)調(diào)用虛擬機(jī)的finalize()方法。在繁忙的程序環(huán)境下,可能導(dǎo)致Java虛擬機(jī)不能有效的使用I/O對(duì)象。
publicvoidprocessFile(StringfilePath){try{FileInputStreamfis=newFileInputStream(filePath);InputStreamReaderisr=newInputStreamReader(fis);BufferedReaderbr=newBufferedReader(isr);Stringline="";while((line=br.readLine())!=null){processLine(line);}}catch(FileNotFoundExceptione){log(e);}catch(IOExceptione){log(e);}}
publicvoidprocessFile(StringfilePath){
try{
FileInputStreamfis=newFileInputStream(filePath);
InputStreamReaderisr=newInputStreamReader(fis);
BufferedReaderbr=newBufferedReader(isr);
Stringline="";
while((line=br.readLine())!=null){
processLine(line);
}
}catch(FileNotFoundExceptione){
log(e);
}catch(IOExceptione){
log(e);
}
}
inputstreamreader示例修復(fù)建議:
應(yīng)在finally塊中釋放I/O對(duì)象。
publicvoidprocessFile(StringfilePath){FileInputStreamfis=null;InputStreamReaderisr=null;BufferedReaderbr=null;try{fis=newFileInputStream(filePath);isr=newInputStreamReader(fis);br=newBufferedReader(isr);Stringline="";while((line=br.readLine())!=null){//processLine(line);}}catch(FileNotFoundExceptione){//log(e);}catch(IOExceptione){//log(e);}finally{if(br!=null){try{br.close();}catch(IOExceptione){//log(e);}}if(isr!=null){try{isr.close();}catch(IOExceptione){//log(e);}}if(fis!=null){try{fis.close();}catch(IOExceptione){//log(e);}}}}
感謝各位的閱讀!關(guān)于“inputstreamreader的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)站標(biāo)題:inputstreamreader的示例分析-創(chuàng)新互聯(lián)
分享URL:http://www.chinadenli.net/article34/dgesse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化、網(wǎng)站改版、自適應(yīng)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容