BufferedReader XX BufferedWriter.

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.

[463 byte] By [badaGflakea] at [2007-11-27 5:09:38]
# 1
> Is this the correct/optimum way?Optimum in what way? Do you care about the line terminators?Kaj
kajbja at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

badaGflakea at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

Hippolytea at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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

kajbja at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 5
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.
badaGflakea at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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

kajbja at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 7
You have enough info to run some code and profile the results. One can discuss it only so long!
Hippolytea at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks kaj and hippolyte.I think I got what I wanted.Will stop the discussion here. :)
badaGflakea at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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.

kevjavaa at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks kevJava.will try that.
badaGflakea at 2007-7-12 10:29:28 > top of Java-index,Java Essentials,Java Programming...