12 Replies Latest reply: May 5, 2010 1:05 PM by 807580 RSS

    How to locate a phrase in a file and read the file after that location

    807580
      I have a text file with the following contents
       Previous date data.....[10,000 lines ]
       same as below but with earlier dates
      
      *******************************************
      * ABC  AutoJournal Utility - Version 2.00 *
      *******************************************
      
      Beginning Reportournal at (time): Tue May 04 18:07:46 IST 2010
      
      AutoJournal assigned updated records a timestamp of (server time):  *2010-05-04*  //THIS IS THE PHRASE  i am looking for
      
      #  Posted Date,           Policy,  Reason, Amount, Insured Name, Agent Name
      1) 2010-04-26 00:00:00.0, 7363496, GRSS, 10.0, KRASSNER W, BROOKS STEVEN E
      2) 2010-04-23 00:00:00.0, 4768200, GRSS, 0.0, STECKLER M, STECKLER GIBSON
      3) 2010-04-20 00:00:00.0, 7328358, GRSS, 4.0, LYON GREGO, WEST MICHAEL J
      4) 2010-04-20 00:00:00.0, 4754236, GRSS, 0.0, PEEBLES JA, HOFFMANN GABRIE
      5) 2010-04-22 00:00:00.0, 7363793, GRSS, 2.0, LAHAYE NAN, GONYEA MICHAEL 
      6) 2010-04-26 00:00:00.0, 7360935, GRSS, 35.0, AMITAY NOA, WIESEL HENRY
      I need to locate the date and start reading the data for the current date only i.e Today's date


      Thus far i have done this....
      it's just the outline
       String srchDate="2010-05-04";
       BufferedReader bf=null;
                     try {
                            bf = new BufferedReader(new FileReader("C:\\File.log"));
                         int lineID = 0;   
                         Pattern pattern =  Pattern.compile(srchDate);   
                         Matcher matcher = null;   
                 
                         String line =null;
      
                            while((line=bf.readLine())!=null){  
                                   System.out.println(line);
      
                                      lineID++;   
                                      matcher = pattern.matcher(line);      //Will match the entire line against srchDate and will return false ...
                                      if( matcher.find()){   
                                          //do stuff...      //stuck here 
                                            //get the line no..
                                     }   
                 
                                   }
                          } catch (Exception e) {
                               
                               e.printStackTrace();
                          }     
      Some help on this would be very helpful...
      Thanks :)

      P.S:This needs to be done using java 1,4 only
        • 1. Re: How to locate a phrase in a file and read the file after that location
          796262
          If I were you, I would split your problem up into smaller pieces.

          Write a method that reads a file into a String.
          Write a method that figures out where in a String a substring occurs (hey, that one might be done for you already...).
          Write a method that returns a substring of a String, based on a start index and a length (something tells me this one might exist already too).
          Write a method that converts a String to the correct datatype.

          Only when you have each of the pieces working separately, should you try to combine them.
          • 2. Re: How to locate a phrase in a file and read the file after that location
            807580
            kevinaworkman wrote:
            If I were you, I would split your problem up into smaller pieces.

            Write a method that reads a file into a String.
            This file is actually quiet huge i am not sure if this would work as the file will only grow in size in the future....


            Write a method that figures out where in a String a substring occurs (hey, that one might be done for you already...).
            Write a method that returns a substring of a String, based on a start index and a length (something tells me this one might exist already too).
            Write a method that converts a String to the correct datatype.
            Regarding these steps i am not sure if i follow

            the lines you see 1)....
            2).... till last line say 800..

            Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
            i am unable to locate this phrase using regex so can you suggest something on that lines

            Only when you have each of the pieces working separately, should you try to combine them.
            i will give that a try as well
            • 3. Re: How to locate a phrase in a file and read the file after that location
              796262
              RainaV wrote:
              kevinaworkman wrote:
              If I were you, I would split your problem up into smaller pieces.

              Write a method that reads a file into a String.
              This file is actually quiet huge i am not sure if this would work as the file will only grow in size in the future....
              How big is "quiet huge"? I wouldn't worry about optimization until you've actually encountered a problem.
              Write a method that figures out where in a String a substring occurs (hey, that one might be done for you already...).
              Write a method that returns a substring of a String, based on a start index and a length (something tells me this one might exist already too).
              Write a method that converts a String to the correct datatype.
              Regarding these steps i am not sure if i follow

              the lines you see 1)....
              2).... till last line say 800..
              I don't understand your question.
              Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
              Okay, and the steps I outlined would work for reading in one line at a time, as well. It's just one more step.
              i am unable to locate this phrase using regex so can you suggest something on that lines
              You're trying to find today's date? Why does that require a regular expression?
              • 4. Re: How to locate a phrase in a file and read the file after that location
                791266
                RainaV wrote:
                Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
                i am unable to locate this phrase using regex so can you suggest something on that lines
                Why store the line in a list? What are you going to do with the data once you have found it?
                • 5. Re: How to locate a phrase in a file and read the file after that location
                  807580
                  kevinaworkman wrote:
                  RainaV wrote:
                  kevinaworkman wrote:
                  If I were you, I would split your problem up into smaller pieces.

                  Write a method that reads a file into a String.
                  This file is actually quiet huge i am not sure if this would work as the file will only grow in size in the future....
                  How big is "quiet huge"? I wouldn't worry about optimization until you've actually encountered a problem.
                  FYI The file size is 20MB ...
                  >
                  Write a method that figures out where in a String a substring occurs (hey, that one might be done for you already...).
                  Write a method that returns a substring of a String, based on a start index and a length (something tells me this one might exist already too).
                  Write a method that converts a String to the correct datatype.
                  Regarding these steps i am not sure if i follow
                  >>
                  the lines you see 1)....
                  2).... till last line say 800..
                  I don't understand your question.
                  I was wondering if your appraoch would be complicated but will give it a try...

                  >
                  Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
                  Okay, and the steps I outlined would work for reading in one line at a time, as well. It's just one more step.
                  i am unable to locate this phrase using regex so can you suggest something on that lines
                  You're trying to find today's date? Why does that require a regular expression?
                  I want to read the data after i have located today's date in the Text file so i need to locate it first but the code i posted doesn't work so i suggested if you can advice on the correction....as per my approach mentioned in the original post


                  But if i do it your way then definately no need for regex i need to store all of it in a string and work woth sub strings so on,...
                  • 6. Re: How to locate a phrase in a file and read the file after that location
                    807580
                    kajbj wrote:
                    RainaV wrote:
                    Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
                    i am unable to locate this phrase using regex so can you suggest something on that lines
                    Why store the line in a list? What are you going to do with the data once you have found it?
                    write it to a excel sheet using apache POI
                    • 7. Re: How to locate a phrase in a file and read the file after that location
                      796262
                      RainaV wrote:
                      But if i do it your way then definately no need for regex i need to store all of it in a string and work woth sub strings so on,...
                      Even if you scan the file one line at a time, you're still storing all of it in a String.

                      Most importantly, no matter how you choose to approach this problem, you should split it up into smaller pieces. Your problem is that you're trying to do everything all at once. Slow down, take the time to get each step right before progressing on to the next one.
                      • 8. Re: How to locate a phrase in a file and read the file after that location
                        791266
                        RainaV wrote:
                        kajbj wrote:
                        RainaV wrote:
                        Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
                        i am unable to locate this phrase using regex so can you suggest something on that lines
                        Why store the line in a list? What are you going to do with the data once you have found it?
                        write it to a excel sheet using apache POI
                        Can't you do that while you are reading?
                        • 9. Re: How to locate a phrase in a file and read the file after that location
                          807580
                          kajbj wrote:
                          RainaV wrote:
                          kajbj wrote:
                          RainaV wrote:
                          Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
                          i am unable to locate this phrase using regex so can you suggest something on that lines
                          Why store the line in a list? What are you going to do with the data once you have found it?
                          write it to a excel sheet using apache POI
                          Can't you do that while you are reading?
                          Can you kinldy elaborate ...

                          thks
                          • 10. Re: How to locate a phrase in a file and read the file after that location
                            807580
                            kevinaworkman wrote:
                            RainaV wrote:
                            But if i do it your way then definately no need for regex i need to store all of it in a string and work woth sub strings so on,...
                            Even if you scan the file one line at a time, you're still storing all of it in a String.

                            Say i do this...
                            StringBuffer sb=new StringBuffer();
                            
                                                                while((line=bf.readLine())!=null){  
                                           sb.append(line);
                                              }
                            
                            //    After reading the complete file....   
                            //    The StringBuffer has the complete file inside it
                                                
                            now the substring methods for sb are asking for a index but i donot have that so how should i proceed on this...
                            actually the "2010-05-04" string how to get it ...?
                            • 11. Re: How to locate a phrase in a file and read the file after that location
                              791266
                              kajbj wrote:
                              RainaV wrote:
                              kajbj wrote:
                              RainaV wrote:
                              Actually i thght i would just read one complete line and store that in an list and then write it to an excel sheet...
                              i am unable to locate this phrase using regex so can you suggest something on that lines
                              Why store the line in a list? What are you going to do with the data once you have found it?
                              write it to a excel sheet using apache POI
                              Can't you do that while you are reading?
                              Read one line. Process it if it's matching, append it to the POI document, then read the next line.
                              • 12. Re: How to locate a phrase in a file and read the file after that location
                                807580
                                kevinaworkman wrote:
                                If I were you, I would split your problem up into smaller pieces.

                                Write a method that reads a file into a String.
                                Write a method that figures out where in a String a substring occurs (hey, that one might be done for you already...).
                                Write a method that returns a substring of a String, based on a start index and a length (something tells me this one might exist already too).
                                Write a method that converts a String to the correct datatype.

                                Only when you have each of the pieces working separately, should you try to combine them.
                                The problem is solved...
                                thanks for the break up i will post the code if anyone comes across the same problem : )

                                thanks to all for their comments on this appreaciate...it