首页 > 编程语言 > 解决java文件流处理异常 mark/reset not supported问题
2020
10-10

解决java文件流处理异常 mark/reset not supported问题

原因:

给定的流不支持mark和reset就会报这个错误。

获取到一个网络流,这个网络流不允许读写头来回移动,也就不允许mark/reset机制.

解决办法:

用BufferedInputStream把原来的流包一层.

BufferedInputStream buffInputStream = new BufferedInputStream(fileInputStream);

补充知识:Java BufferedReader之mark和reset方法实践

在读取文本的操作中,常常会在读取到文件末尾时重新到文件开头进行操作。通过搜索发现,有两种方法:

(1)mark和reset方法,但是在博客中都是以简短的string为示例对象;

(2)利用randomacessfile中的seek方法,seek方法可进行移动。

由于前面的文本操作使用了BufferedReader,所以只能用mark和reset方法将程序进行完善。非常好理解这两个方法,一个在前面做标记,另一个重置返回到做标记的位置。

首先,看一下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)方法仅有一个参数,翻译过来就是“保证mark有效的情况下限制读取的字符数。当 读取字符达到或超过此限制时,尝试重置流会失败。当限制数值大于输入buffer的默认大小时,将会动态分配一个容量不小于限制数值的buffer。因此,应该慎用大数值。”

第二,获取文件的大小

既然要读取某文件,需知道该文件的大小,调用file.length()方法,将会“Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.”

由于返回数值为long型,需加一个判断(是否超出int默认最大值,因为mark方法的参数为int类型)后才能进行int的强制转换

int size;
if(filesize>=2147483647){
  Toast.makeText(……).show();
}else{
  size=(int)filesize;
}

第三,设置mark参数

如果完成前两步后,并mark(size)你就去尝试,那么还会出错,为什么呢?

前面的mark()方法已经讲过“当读取字符达到或超过此限制时,尝试重置流会失败”,所以最好还要将mark的size数值加1.

解决。

PS:我尝试了270多KB的文件,也可以正常读取。

修改---2016-07-19 17:03

吃完午饭回来后,就意识到一个问题,重复操作io是非常浪费资源的,为何不将数据全部写入list或map中,这样就是从缓存中读取数据,操作更快一点。一下午都在修改程序,包括输出部分及排序操作,总之对这部分的理解加深了许多。

编程技巧