2 Replies Latest reply: May 29, 2012 6:07 AM by gimbal2 RSS

    problem with mark file

    940279
      Hi everyone!! I am reading characters from a file, and need to mark (with mark) a specific word in the file, read more characters, and the come back to the mark (with reset).

      This is a snapshot of the code (without try catch):
                     FileInputStream fstream = new FileInputStream(url_dic);
                     DataInputStream in = new DataInputStream(fstream);
                     BufferedReader br = new BufferedReader(new InputStreamReader(in));
                     int number_words=0;
                     String strLine;
                     do
                     {
                          br.mark(1);
                          strLine=br.readLine();
                     }while(strLine.length()<5);
                     
                     do
                     {
                          strLine=br.readLine();
                          number_words++;
                     }while(strLine.length()== this.long_word);
                     System.out.println("last word="+strLine);
                     br.reset();
      But when the file has too many characters, I get this exception: “Mark invalid”

      Why?? can someone please explain how mark works??

      Thank you very very much
        • 1. Re: problem with mark file
          EJP
          You are calling mark(1), which sets a read-ahead limit of 1, then calling readLine(), which is almost certainly going to need to read more than one character.

          I suggest another look at the Javadoc. There's not much point in writing arbitrary code and wondering why it doesn't work.
          • 2. Re: problem with mark file
            gimbal2
            In stead of running to this forum right away, why don't you take a look at the javadoc for the mark() method of BufferedReader?

            http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#mark%28int%29

            Especially the part about that integer parameter for which you are now passing 1.