解決java文件流處理異常 mark/reset not supported問題
原因:
給定的流不支持mark和reset就會報這個錯誤。
獲取到一個網(wǎng)絡(luò)流,這個網(wǎng)絡(luò)流不允許讀寫頭來回移動,也就不允許mark/reset機(jī)制.
解決辦法:
用BufferedInputStream把原來的流包一層.
BufferedInputStream buffInputStream = new BufferedInputStream(fileInputStream);
補(bǔ)充知識:Java BufferedReader之mark和reset方法實踐
在讀取文本的操作中,常常會在讀取到文件末尾時重新到文件開頭進(jìn)行操作。通過搜索發(fā)現(xiàn),有兩種方法:
(1)mark和reset方法,但是在博客中都是以簡短的string為示例對象;
(2)利用randomacessfile中的seek方法,seek方法可進(jìn)行移動。
由于前面的文本操作使用了BufferedReader,所以只能用mark和reset方法將程序進(jìn)行完善。非常好理解這兩個方法,一個在前面做標(biāo)記,另一個重置返回到做標(biāo)記的位置。
首先,看一下mark方法
public void mark(int readAheadLimit) throws IOExceptionMarks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.Overrides:markin class ReaderParameters:readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.Throws:IllegalArgumentException- If readAheadLimit is < 0IOException- If an I/O error occurs
mark(readAheadLimit)方法僅有一個參數(shù),翻譯過來就是“保證mark有效的情況下限制讀取的字符數(shù)。當(dāng) 讀取字符達(dá)到或超過此限制時,嘗試重置流會失敗。當(dāng)限制數(shù)值大于輸入buffer的默認(rèn)大小時,將會動態(tài)分配一個容量不小于限制數(shù)值的buffer。因此,應(yīng)該慎用大數(shù)值?!?/p>
第二,獲取文件的大小
既然要讀取某文件,需知道該文件的大小,調(diào)用file.length()方法,將會“Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.”
由于返回數(shù)值為long型,需加一個判斷(是否超出int默認(rèn)最大值,因為mark方法的參數(shù)為int類型)后才能進(jìn)行int的強(qiáng)制轉(zhuǎn)換
int size;if(filesize>=2147483647){ Toast.makeText(……).show();}else{ size=(int)filesize;}
第三,設(shè)置mark參數(shù)
如果完成前兩步后,并mark(size)你就去嘗試,那么還會出錯,為什么呢?
前面的mark()方法已經(jīng)講過“當(dāng)讀取字符達(dá)到或超過此限制時,嘗試重置流會失敗”,所以最好還要將mark的size數(shù)值加1.
解決。
PS:我嘗試了270多KB的文件,也可以正常讀取。
修改---2016-07-19 17:03
吃完午飯回來后,就意識到一個問題,重復(fù)操作io是非常浪費資源的,為何不將數(shù)據(jù)全部寫入list或map中,這樣就是從緩存中讀取數(shù)據(jù),操作更快一點。一下午都在修改程序,包括輸出部分及排序操作,總之對這部分的理解加深了許多。
相關(guān)文章:
1. php測試程序運行速度和頁面執(zhí)行速度的代碼2. ASP中常用的22個FSO文件操作函數(shù)整理3. 三個不常見的 HTML5 實用新特性簡介4. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. SharePoint Server 2019新特性介紹7. React+umi+typeScript創(chuàng)建項目的過程8. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析10. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
