13 Replies Latest reply: Jan 10, 2007 5:01 AM by 807603 RSS

    How to parse a text file?

    807603
      Hi,


      I have a text file which has this kind of format, Each row will contain this kind of data,

      number
      name of user including space
      phone
      age
      brith date in this format 1970 6 14

      So a sample row will be like
      111 jed difkins 012-12512-321 35 1970 6 1


      What is the best way for parsing this file? I need to separate fields correctly and the do required things on this data.

      Any help is appreciated very much.

      Thanks.
        • 1. Re: How to parse a text file?
          800322
          What happens with
          111 Rene Armand Chennault 012-12512-321 35 1970 6 1
          ?
          • 2. Re: How to parse a text file?
            807603
            You can use a java.util.Scanner.

            Personally, I'd do it like this:

            1) read the file line-by-line. You can use Scanner for this, or java.io.BufferedReader.

            2) make parsing a single line part of the class that encapsulates this information. Suppose all this data is for a Person class. Then in Person.java you'd have a constructor or method that looks like this:
            public Person(String formattedString);
            //or
            public static Person createPersonObjectFromFormattedDataString(String formattedString);
            (you can use a shorter method name there, of course)
            Altternatively, you could create a PersonFactory class in the same package as Person.

            Actually you could put the stuff in part (1) in the same package (or source file) as well.

            One you get a line of data, you can use regular expressions, or Scanner again, or StringTokenizer, or other stuff. I'd start with regexps though (java.util.regex).
            • 3. Re: How to parse a text file?
              800322
              If the "rows" are separated by tabs, you could as well simply split on those. The date strings can be parsed using SimpleDateFormat.
              • 4. Re: How to parse a text file?
                807603
                        String line = "111 Rene Armand Chennault 012-12512-321 35 1970 6 1 ";
                        String[] splitLine = line.split("(?<=\\d)\\s+|\\s+(?=\\d)");
                        for (String field : splitLine)
                        {
                            System.out.println(field);
                        }
                • 5. Re: How to parse a text file?
                  807603
                  you can use BufferedReader.readLine() method
                  • 6. Re: How to parse a text file?
                    807603
                    Thanks very much to all of you,

                    For some time I've been busy with j2me and have forgotten some j2se tools like regex package.
                    You are right it seems the regex is the best way to go.
                    I don't want to seem ungrateful or any thing but actually I'm not so good in creating regex patterns and I have a question, Is it possible to read for example date section completely as a string not separate tokens and also it seems it will have a time section with ":" character. I think it must be possible but I'm not sure.


                    Any how thanks very much.
                    Thanks.
                    • 7. Re: How to parse a text file?
                      807603
                      you may use CSV file for your datas and create a CSVParser (using the tips given in this thread to read the file), it will be better than a txt file (or XML...)
                      • 8. Re: How to parse a text file?
                        800322
                        I don't want to seem ungrateful or any thing but
                        actually I'm not so good in creating regex patterns
                        and I have a question, Is it possible to read for
                        example date section completely as a string not
                        separate tokens
                        Already happens with sabre's regex.
                        and also it seems it will have a time
                        section with ":" character. I think it must be
                        possible but I'm not sure.
                        Yes it is.
                        • 9. Re: How to parse a text file?
                          807603
                          Question: why do you store age and birth date? It is not better store birth date and calculate age where object is build?
                          • 10. Re: How to parse a text file?
                            807603
                            you may use CSV file for your datas and create a
                            CSVParser (using the tips given in this thread to
                            read the file),
                            Why re-invent the wheel - http://ostermiller.org/utils/CSV.html ?
                            • 11. Re: How to parse a text file?
                              807603
                              you may use CSV file for your datas and create a
                              CSVParser (using the tips given in this thread to
                              read the file),
                              Why re-invent the wheel -
                              http://ostermiller.org/utils/CSV.html ?
                              i don't want to re-invent the wheel, i just don't know com.Ostermiller.util Java Utilities !
                              thanks for the info
                              • 12. Re: How to parse a text file?
                                807603
                                my text file is
                                303 04222530987 12/12/2007 12:32:23 00:01:00 2
                                302 04222492528 12/25/2007 12:52:36 00:12:32 3

                                how can i separate the above datas in the file into the folloving datas
                                calling 303
                                called 04222530987
                                date 12/12/2007
                                time 12:32:23
                                duration 00:10:00
                                junction 2
                                please rply me with sample coding

                                any comments is honestly appreciated
                                thankyou
                                • 13. Re: How to parse a text file?
                                  807603
                                  Read the java.lang.String API. It offers lot of useful methods, under each a split() method.

                                  http://www.google.com/search?q=java.lang.string+api+javase+site:sun.com

                                  And in the future, please start new topic for each independent question instead of hijacking others topics and resurrecting old topics.