10 Replies Latest reply: Mar 7, 2009 1:05 PM by camickr RSS

    how to read text file line by line...?

    807588
      how to read text file line by line, but the linefeed is defined by user, return list of string, each line of file is a item of list?
      please help me.
      Thanks very much
        • 1. Re: how to read text file line by line...?
          796440
          [http://java.sun.com/docs/books/tutorial/essential/io/]
          • 2. Re: how to read text file line by line...?
            807588
            In Groovy, you would do something like:
            linefeed = "\n" //or "\r\n" if the user chose so
            lines = new File('pathtofile').text.split("${linefeed}")
            This is one of the things that has always annoyed me about Sun's sdk, i.e. the lack of easy ways to do things like that. You always end up making your own utilities or use something like Apache's commons.io. Same goes for jdbc and xml - I'll wait for appropriate topics to show how easy that is in Groovy :)

            Edited by: Brynjar on 7.3.2009 12:42
            • 3. Re: how to read text file line by line...?
              JoachimSauer
              Brynjar wrote:
              In Groovy, you would do something like:
              linefeed = "\n" //or "\r\n" if the user chose so
              lines = new File('pathtofile').text.split("${linefeed}")
              This is one of the things that has always annoyed me about Sun's sdk, i.e. the lack of easy ways to do things like that. You always end up making your own utilities or use something like Apache's commons.io. Same goes for jdbc and xml - I'll wait for appropriate topics to show how easy that is in Groovy :)
              I generally agree, but what I really don't like about the Groovy text-file handling niceties: They don't care about encoding/always use the default encoding. And as soon as you want to specify the encoding, it gets a lot more complex (granted, it's still easier than in Java).
              • 4. Re: how to read text file line by line...?
                807588
                JoachimSauer wrote:
                I generally agree, but what I really don't like about the Groovy text-file handling niceties: They don't care about encoding/always use the default encoding. And as soon as you want to specify the encoding, it gets a lot more complex (granted, it's still easier than in Java).
                That's true, but it should be easy for the Groovy creators to add an easy way for this too - remember that Groovy is still very young. IMO it should look something like this when you want to specify encoding:
                filetext = new File('path').text('UTF-8')
                • 5. Re: how to read text file line by line...?
                  JoachimSauer
                  Brynjar wrote:
                  JoachimSauer wrote:
                  I generally agree, but what I really don't like about the Groovy text-file handling niceties: They don't care about encoding/always use the default encoding. And as soon as you want to specify the encoding, it gets a lot more complex (granted, it's still easier than in Java).
                  That's true, but it should be easy for the Groovy creators to add an easy way for this too - remember that Groovy is still very young. IMO it should look something like this when you want to specify encoding:
                  filetext = new File('path').text('UTF-8')
                  That would be really nice.
                  • 6. Re: how to read text file line by line...?
                    807588
                    thanks Brynjar
                    But I want to use java library to implement it, as FileInputStream, BufferReader....
                    Because in my project doesn't use Groovy.
                    please help me.
                    • 7. Re: how to read text file line by line...?
                      camickr
                      In Groovy, you would do something like:
                      Well, the question is not about Groovy.
                      the lack of easy ways to do things like that
                      A quick read of the API comes up with Scanner.useDelimiter(...).
                      • 8. Re: how to read text file line by line...?
                        807588
                        camickr wrote:
                        In Groovy, you would do something like:
                        Well, the question is not about Groovy.
                        The Java question was already answered by jverd, ie link to a tutorial. Besides, the OP could embed Groovy in his app if he wanted to.

                        >
                        the lack of easy ways to do things like that
                        A quick read of the API comes up with Scanner.useDelimiter(...).
                        It's easier now than a few years ago, but still more clunky than it needs to be.
                        • 9. Re: how to read text file line by line...?
                          camickr
                          Besides, the OP could embed Groovy in his app if he wanted to.
                          And why would you want to learn (and download) another API, when Java already easily supports the requirement?
                          • 10. Re: how to read text file line by line...?
                            807588
                            phamtrungkien wrote:
                            thanks Brynjar
                            But I want to use java library to implement it, as FileInputStream, BufferReader....
                            Because in my project doesn't use Groovy.
                            please help me.
                            Look at the javadoc for java.util.Scanner and its nextLine method. Just loop using that and add each line to e.g. an ArrayList. As for Groovy, maybe its not applicable for you, especially if this is a homework assignment. But it's much more fun.