2012-11-02 13 views
11

mark() और reset() कैसे ठीक से काम कर रहे हैं (नीचे कोड में), चरण-दर-चरण? मैंने अपना खुद का उदाहरण लिखने की कोशिश की लेकिन गलत मार्क अपवाद या उस तरह के समान फेंकना शुरू हो गया है, और मैं समझ नहीं पा रहा हूं कि इस कोड में चिह्न लगाने और रीसेट करने के तरीकों का क्या मतलब है क्योंकि मुझे इस के साथ या बिना अंतर दिखाई नहीं देता है।इनपुटस्ट्रीम, मार्क(), रीसेट()

import java.io.*; 

class BufferedInputStreamDemo { 
    public static void main(String args[]) { 
     String s = "© is a copyright symbol, " 
       + "however &copy isn't.\n"; 
     byte buf[] = s.getBytes(); 

     ByteArrayInputStream in = new ByteArrayInputStream(buf); 
     int c; 
     boolean marked = false; 

     //try_with_resources 
     try (BufferedInputStream f = new BufferedInputStream(in)) { 
      while ((c = f.read()) != -1) { 
       switch (c) { 
        case '&': 
         if (!marked) { 
          f.mark(32); 
          marked = true; 
         } else { 
          marked = false; 
         } 
         break; 
        case ';': 
         if (marked) { 
          marked = false; 
          System.out.print("(c)"); 
         } else 
          System.out.print((char) c); 
         break; 
        case ' ': 
         if (marked) { 
          marked = false; 
          f.reset(); 
          System.out.print("&"); 
         } else 
          System.out.print((char) c); 
         break; 
        default: 
         if (!marked) 
          System.out.print((char) c); 
         break; 
       } 
      } 
     } catch (IOException e) { 
      System.out.println("I/O Error: " + e); 
     } 
    } 
} 
+1

क्या अपवाद फेंक दिया जा रहा है? क्या आपके पास एक स्टैक ट्रेस है? कृपया, अपने प्रश्न पर अधिक विशिष्ट हो। – Lion

+0

अपवाद को फेंक दिया गया है 'I/O त्रुटि: java.io.IOException: अमान्य चिह्न पर रीसेट करना', लेकिन मैंने लिखा कि उस अपवाद को किसी अन्य उदाहरण में फेंक दिया जा रहा है। मुख्य रूप से मैं पूछ रहा हूं कि चिह्न क्या है (32) और ** इस उदाहरण ** में रीसेट करना। – ashur

+0

@ user1165499 इस कोड को कैसे काम कर रहा है, इस बारे में विवरण के लिए मेरा उत्तर देखें, और मुझे आपके अन्य उदाहरण में समस्या का संदेह है। – dan

उत्तर

6

जब f.mark(32); तक पहुँचने है संपादित करें कर्सर & के बाद पहले से ही है, और एक मार्कर reset के लिए सेट है यह जानने के लिए कि वापस कहां कूदना है। तो जब आपको पता चला है कि ; तत्व को बंद करने के लिए अनुपलब्ध है, तो आप & मैन्युअल रूप से प्रिंट कर रहे हैं और reset विधि का उपयोग करके कॉल का उपयोग करके, & के बाद मार्कर रखा गया था, जहां कर्सर को रखा गया था)। अगले पढ़ने पर, क्योंकि आपका marked वैरिएबल सेट नहीं है, यह वर्णों को प्रिंट करेगा।

mark(32) का अर्थ मार्कर को स्वचालित रूप से हटाने का है यदि आपका पढ़ा कर्सर 32 वर्ण से अधिक आगे बढ़ेगा। यह आपके अन्य कोड में समस्या हो सकती है, जो एक त्रुटि को ट्रिगर कर रही है, क्योंकि मार्कर पहले ही अमान्य हो गया था।

4

API दस्तावेज़ देखें:

mark(int)

Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

This method simply performs in.mark(readlimit).

reset()

Repositions this stream to the position at the time the mark method was last called on this input stream.

This method simply performs in.reset().

Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.

संबंधित मुद्दे