<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Vincent Jia 博客

    to be a better man, to be a bad man.

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      29 隨筆 :: 3 文章 :: 0 評(píng)論 :: 0 Trackbacks
    相信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);
        }
    }

    posted on 2010-03-29 16:13 iLinux 閱讀(4858) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产亚洲精品91| 日韩在线不卡免费视频一区| 国产亚洲?V无码?V男人的天堂| 精品视频一区二区三区免费| 久久夜色精品国产噜噜噜亚洲AV| AV片在线观看免费| 国产jizzjizz免费视频| a级男女仿爱免费视频| 亚洲人成www在线播放| 亚洲色偷偷综合亚洲AVYP| 国产日本一线在线观看免费| 一级中文字幕免费乱码专区 | 亚洲美女视频一区二区三区| 免费看韩国黄a片在线观看| 亚洲日韩在线观看免费视频| 亚洲av无码专区在线| 亚洲一区二区视频在线观看 | 成年女人免费v片| 在线成人精品国产区免费| 亚洲精品第一综合99久久| 亚洲人成网77777亚洲色| 日韩视频在线免费| 色欲A∨无码蜜臀AV免费播 | a级毛片免费播放| 国产精品无码亚洲精品2021| 亚洲视频在线观看免费视频| 国产亚洲老熟女视频| 国产区卡一卡二卡三乱码免费| 99re在线免费视频| 精品国产免费一区二区三区| 亚洲国产高清国产拍精品| 亚洲美女视频一区| 亚洲精品无码久久久久sm| 免费jlzzjlzz在线播放视频| 性xxxxx免费视频播放| 99久久免费精品视频| a视频在线免费观看| 一个人看的www免费高清| 久久亚洲中文无码咪咪爱| 亚洲一区二区三区高清不卡| 久久亚洲国产精品成人AV秋霞|