原因:
給定的流不支持mark和reset就會(huì)報(bào)這個(gè)錯(cuò)誤。
獲取到一個(gè)網(wǎng)絡(luò)流,這個(gè)網(wǎng)絡(luò)流不允許讀寫(xiě)頭來(lái)回移動(dòng),也就不允許mark/reset機(jī)制.
解決辦法:
用BufferedInputStream把原來(lái)的流包一層.
BufferedInputStream buffInputStream = new BufferedInputStream(fileInputStream);
補(bǔ)充知識(shí):Java BufferedReader之mark和reset方法實(shí)踐
在讀取文本的操作中,常常會(huì)在讀取到文件末尾時(shí)重新到文件開(kāi)頭進(jìn)行操作。通過(guò)搜索發(fā)現(xiàn),有兩種方法:
(1)mark和reset方法,但是在博客中都是以簡(jiǎn)短的string為示例對(duì)象;
(2)利用randomacessfile中的seek方法,seek方法可進(jìn)行移動(dòng)。
由于前面的文本操作使用了BufferedReader,所以只能用mark和reset方法將程序進(jìn)行完善。非常好理解這兩個(gè)方法,一個(gè)在前面做標(biāo)記,另一個(gè)重置返回到做標(biāo)記的位置。
首先,看一下mark方法
public void mark
(int readAheadLimit) throws IOException
Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
Overrides:
markin class Reader
Parameters:
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 < 0
IOException- If an I/O error occurs
mark(readAheadLimit)方法僅有一個(gè)參數(shù),翻譯過(guò)來(lái)就是“保證mark有效的情況下限制讀取的字符數(shù)。當(dāng) 讀取字符達(dá)到或超過(guò)此限制時(shí),嘗試重置流會(huì)失敗。當(dāng)限制數(shù)值大于輸入buffer的默認(rèn)大小時(shí),將會(huì)動(dòng)態(tài)分配一個(gè)容量不小于限制數(shù)值的buffer。因此,應(yīng)該慎用大數(shù)值。”
第二,獲取文件的大小
既然要讀取某文件,需知道該文件的大小,調(diào)用file.length()方法,將會(huì)“Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.”
由于返回?cái)?shù)值為long型,需加一個(gè)判斷(是否超出int默認(rèn)最大值,因?yàn)閙ark方法的參數(shù)為int類(lèi)型)后才能進(jìn)行int的強(qiáng)制轉(zhuǎn)換
1
2
3
4
5
6
|
int size; if (filesize>= 2147483647 ){ Toast.makeText(……).show(); } else { size=( int )filesize; } |
第三,設(shè)置mark參數(shù)
如果完成前兩步后,并mark(size)你就去嘗試,那么還會(huì)出錯(cuò),為什么呢?
前面的mark()方法已經(jīng)講過(guò)“當(dāng)讀取字符達(dá)到或超過(guò)此限制時(shí),嘗試重置流會(huì)失敗”,所以最好還要將mark的size數(shù)值加1.
解決。
PS:我嘗試了270多KB的文件,也可以正常讀取。
修改---2016-07-19 17:03
吃完午飯回來(lái)后,就意識(shí)到一個(gè)問(wèn)題,重復(fù)操作io是非常浪費(fèi)資源的,為何不將數(shù)據(jù)全部寫(xiě)入list或map中,這樣就是從緩存中讀取數(shù)據(jù),操作更快一點(diǎn)。一下午都在修改程序,包括輸出部分及排序操作,總之對(duì)這部分的理解加深了許多。
原文鏈接:https://blog.csdn.net/huangdingsheng/article/details/93400975