Vincent Jia 博客

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            29 隨筆 :: 3 文章 :: 0 評論 :: 0 Trackbacks
          相信BufferedReader應該是大家所熟悉的一個操作類,但是其中的mark,reset方法,不知大家是否有過關(guān)注,
          近日工作中碰到問題,不解,所以就Google并記錄下來,給自己個記錄,也希望與大家分享。
          關(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

          在項目中有如下代碼:
           
              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的功能是有很多用處的,比如統(tǒng)計文件行數(shù),在html中讀取發(fā)現(xiàn)tag后再將文件指針返回指向tag前面的位置;
          1.在上面的code中,我查閱后(原作者已離職)的理解是:此處就是要在當前處mark一下,讀取下一個char后,判斷是否'<',重復畫線處,直到發(fā)現(xiàn)'<',然后返回tag前的文本;則此處的9(就是這個9害我思索許久),不一定是9,可以是8,7...2,后面只讀一次就又mark了; 1不行(后面討論)。
          2.在文件讀取中,使用mark方法時,要注意,要設(shè)置mark參數(shù)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 - 在仍保留該標記的情況下,對可讀取字符數(shù)量的限制。在讀取達到或超過此限制的字符后,嘗試重置流可能會失敗。限制值大于輸入緩沖區(qū)的大小將導致分配一個新緩沖區(qū),其大小不小于該限制值。因此應該小心使用較大的值。 //就是建議使用大于最大值的值

          給大家一段代碼可以參考運行:

          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 閱讀(4862) 評論(0)  編輯  收藏

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 收藏| 宝丰县| 彰化县| 北安市| 镇宁| 两当县| 醴陵市| 陇西县| 黎川县| 林周县| 勃利县| 大冶市| 万源市| 渭南市| 乐至县| 澄迈县| 治县。| 万州区| 普安县| 拉孜县| 渭源县| 湄潭县| 孝昌县| 射阳县| 湟源县| 蓬安县| 梁平县| 宿迁市| 尼玛县| 松原市| 隆昌县| 罗源县| 安塞县| 灵石县| 焦作市| 香格里拉县| 南召县| 梓潼县| 临沧市| 德令哈市| 江都市|