相信BufferedReader應該是大家所熟悉的一個操作類,但是其中的mark,reset方法,不知大家是否有過關注,
近日工作中碰到問題,不解,所以就Google并記錄下來,給自己個記錄,也希望與大家分享。
關于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
關于它的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
在項目中有如下代碼:
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文件解析中,讀取當前BufferedReader至第一個tag。
其實,BufferedReader的功能是有很多用處的,比如統計文件行數,在html中讀取發現tag后再將文件指針返回指向tag前面的位置;
1.在上面的code中,我查閱后(原作者已離職)的理解是:此處就是要
在當前處mark一下,讀取下一個char后,判斷是否'<',重復畫線處,直到發現'<',然后返回tag前的文本;則此處的9(就是這個9害我思索許久),不一定是9,可以是8,7...2,后面只讀一次就又mark了; 1不行(后面討論)。
2.在文件讀取中,使用mark方法時,要注意,要設置mark參數int readAheadLimit=file.length + 1,否則就會爆出異常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,來看中文的:
readAheadLimit - 在仍保留該標記的情況下,對可讀取字符數量的限制。在
讀取達到或超過此限制的字符后,嘗試重置流可能會失敗。限制值大于輸入緩沖區的大小將導致分配一個新緩沖區,其大小不小于該限制值。因此應該小心使用較大的值。 //就是建議使用大于最大值的值
給大家一段代碼可以參考運行:
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);
}
}