相信BufferedReader應(yīng)該是大家所熟悉的一個(gè)操作類(lèi),但是其中的mark,reset方法,不知大家是否有過(guò)關(guān)注,
近日工作中碰到問(wèn)題,不解,所以就Google并記錄下來(lái),給自己個(gè)記錄,也希望與大家分享。
關(guān)于BufferedReader:
public class BufferedReader
extends Reader
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in
= new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.
Since:
JDK1.1
See Also:
FileReader, InputStreamReader
關(guān)于它的mark,reset方法:
mark
public void mark(int readAheadLimit)
throws IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
Overrides:
mark in class Reader
Parameters:
readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream 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
reset
public void reset()
throws IOException
Reset the stream to the most recent mark.
Overrides:
reset in class Reader
Throws:
IOException - If the stream has never been marked, or if the mark has been invalidated
在項(xiàng)目中有如下代碼:
protected static String readToTag(BufferedReader br)
{
String string = "";
try
{
br.mark(9);
int charVal = br.read();
while (charVal != '<' && !isFileEnd(br))
{
if(charVal == '\r'){
currentLineNo ++;
}
string += (char)charVal;
br.mark(9);
charVal = br.read();
}
br.reset();
if (isFileEnd(br) && charVal>0)
{
string += (char)charVal;
}
return (string);
}
catch (IOException ioe)
{
Message.show(Message.error, ioe.getMessage());
return (null);
}
}
其功能是:在html文件解析中,讀取當(dāng)前BufferedReader至第一個(gè)tag。
其實(shí),BufferedReader的功能是有很多用處的,比如統(tǒng)計(jì)文件行數(shù),在html中讀取發(fā)現(xiàn)tag后再將文件指針?lè)祷刂赶騮ag前面的位置;
1.在上面的code中,我查閱后(原作者已離職)的理解是:此處就是要
在當(dāng)前處mark一下,讀取下一個(gè)char后,判斷是否'<',重復(fù)畫(huà)線處,直到發(fā)現(xiàn)'<',然后返回tag前的文本;則此處的9(就是這個(gè)9害我思索許久),不一定是9,可以是8,7...2,后面只讀一次就又mark了; 1不行(后面討論)。
2.在文件讀取中,使用mark方法時(shí),要注意,要設(shè)置mark參數(shù)int readAheadLimit=file.length + 1,否則就會(huì)爆出異常java.io.IOException: Mark invalid.
原因在于:
jdk中聲明:
readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After
reading this many characters, attempting to reset the stream 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.
英文聲明可能有些confused,來(lái)看中文的:
readAheadLimit - 在仍保留該標(biāo)記的情況下,對(duì)可讀取字符數(shù)量的限制。在
讀取達(dá)到或超過(guò)此限制的字符后,嘗試重置流可能會(huì)失敗。限制值大于輸入緩沖區(qū)的大小將導(dǎo)致分配一個(gè)新緩沖區(qū),其大小不小于該限制值。因此應(yīng)該小心使用較大的值。 //就是建議使用大于最大值的值
給大家一段代碼可以參考運(yùn)行:
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
String s = "Message.show(Message.error, ioe.getMessage()).一";
char buf[] = new char[s.length()];
s.getChars(0, s.length(), buf, 0);
CharArrayReader in = new CharArrayReader(buf);
BufferedReader f = new BufferedReader(in);
String d = "";
int c;
System.out.println(s.length() );
f.mark(s.length() +1);
while ((c = f.read()) != -1) {
d += (char)c;
}
f.reset();
System.out.println(d);
}
}