10 Replies Latest reply: May 22, 2007 4:05 PM by 807606 RSS

    BufferedReader XX BufferedWriter.

    807606
      Hi,

      I need to read a BufferedReader and then write to a File.
      I got the BufferedReader by reading a CLOB from the database.

      Now, to write it to a file, I'm using BufferedWriter.

      I'm using the readLine method of BufferedReader and creating a stringbuffer, converting it to a string and then writing the string to a file using the write(String) method of BufferedWriter.

      Is this the correct/optimum way?

      Please comment.
        • 1. Re: BufferedReader XX BufferedWriter.
          791266
          Is this the correct/optimum way?
          Optimum in what way? Do you care about the line terminators?

          Kaj
          • 2. Re: BufferedReader XX BufferedWriter.
            807606
            Since that was HTML content in it, I believe it wouldn't be a problem in displaying the file on a browser.
            Thatz the reason I didn't consider line terminators.
            Pls. correct me if I'm wrong. (Pls. suggest how to proceed, if we need line terminators).

            By asking abt the optimum way, I was referring to :
            Will that be better to use char[] to read and write, rather than using readline and making it a string and using write.

            Sorry for the ambiguity.
            • 3. Re: BufferedReader XX BufferedWriter.
              807606
              I would take the Reader that Clob's getCharacterStream returns and
              repeatedly use the Reader's

              int read(char[] cbuf)

              method to put data into a char[] buffer (say of length 0x1000) and write
              that using the corresponding write method of FileWriter.

              Your method seems to do too much copying from buffer to buffer to buffer.

              -- that being said, the overhead for doing that isn't terribly high.
              If you profile it, the timing will be similar.
              • 4. Re: BufferedReader XX BufferedWriter.
                791266
                Since that was HTML content in it, I believe it
                wouldn't be a problem in displaying the file on a
                browser.
                Thatz the reason I didn't consider line terminators.
                Pls. correct me if I'm wrong. (Pls. suggest how to
                proceed, if we need line terminators).

                By asking abt the optimum way, I was referring to :
                Will that be better to use char[] to read and write,
                rather than using readline and making it a string and
                using write.

                Sorry for the ambiguity.
                It might be good enought for you. I would try to read and write strings. The BufferedWriter has a newLine method.

                Kaj
                • 5. Re: BufferedReader XX BufferedWriter.
                  807606
                  Thanks kaj.
                  But, my question still remains.

                  which one is optimum way of doing it? (ofcourse, without considering the line terminators).
                  either readline and newline or using character arrays, as our friend posted previously.
                  • 6. Re: BufferedReader XX BufferedWriter.
                    791266
                    Thanks kaj.
                    But, my question still remains.

                    which one is optimum way of doing it? (ofcourse,
                    without considering the line terminators).
                    either readline and newline or using character
                    arrays, as our friend posted previously.
                    It can depend on the stream that you are reading from, but it might be better to read char[] of a few KB from the stream.

                    Kaj
                    • 7. Re: BufferedReader XX BufferedWriter.
                      807606
                      You have enough info to run some code and profile the results. One can discuss it only so long!
                      • 8. Re: BufferedReader XX BufferedWriter.
                        807606
                        Thanks kaj and hippolyte.
                        I think I got what I wanted.
                        Will stop the discussion here. :)
                        • 9. Re: BufferedReader XX BufferedWriter.
                          807606
                          which one is optimum way of doing it? (ofcourse,
                          without considering the line terminators).
                          either readline and newline or using character
                          arrays, as our friend posted previously.
                          You won't like my answer...

                          If you use the profile switch for the JVM...
                          java -Xprof main
                          It will tell you how long it's taking to do something, how much time it's spending in certain function calls. Benchmark it once, tinker with it, and get it to where you're comfortable with its performance.
                          • 10. Re: BufferedReader XX BufferedWriter.
                            807606
                            Thanks kevJava.
                            will try that.