<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      29 隨筆 :: 3 文章 :: 0 評論 :: 0 Trackbacks
    相信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);
        }
    }

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

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 成年女人毛片免费播放人| 一区二区免费视频| 精品久久洲久久久久护士免费| 亚洲同性男gay网站在线观看| 无码精品国产一区二区三区免费| 亚洲AV日韩AV永久无码久久| 久久免费高清视频| 亚洲电影国产一区| 18禁美女黄网站色大片免费观看| 亚洲大香伊人蕉在人依线| 91频在线观看免费大全| 亚洲人成无码网站在线观看| 国产精品麻豆免费版| 免费国产a理论片| 亚洲人成中文字幕在线观看| 日韩精品无码专区免费播放| 亚洲人成电影网站| 性做久久久久免费看| 一级毛片a女人刺激视频免费 | 亚洲爆乳精品无码一区二区三区| 免费无码又爽又刺激网站| 亚洲国产精品久久久久| 精品久久久久久久久免费影院| 亚洲欧洲无码一区二区三区| 亚洲精品尤物yw在线影院| 国产一级片免费看| 国产 亚洲 中文在线 字幕| 午夜国产羞羞视频免费网站| a级成人免费毛片完整版| 91嫩草亚洲精品| 免费va在线观看| 国产成人精品免费久久久久 | 亚洲国产aⅴ成人精品无吗| 亚洲精品综合久久| 国产人成免费视频网站| 激情小说亚洲色图| 亚洲第一区视频在线观看| 国产jizzjizz视频全部免费| 日韩免费无码一区二区三区| 青草久久精品亚洲综合专区| 日韩亚洲人成在线综合日本|