您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(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ò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。