| Author |
What is the fastest way to write a file?
|
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
Hi,
Could any one please clarify me, what is the fastest way(in terms of performance) to write a file in java? I am not sure that the BufferedWriter is the best one to pick if the performance is in mind.
|
Life is either daring something or nothing - Helen Keller
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
|
performance is the last thing you should be worried about. Disk i/o is going to be slow no matter what. So unless you have some well documented need for performance, and you know that it is the writing of the file that is your bottleneck, you're wasting your time trying to micro-optimize it.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
fred rosenberger wrote:performance is the last thing you should be worried about. Disk i/o is going to be slow no matter what. So unless you have some well documented need for performance, and you know that it is the writing of the file that is your bottleneck, you're wasting your time trying to micro-optimize it.
Thanks for your reply. I have completly understood your point. However I am eager to know that which one of the i/o utilities in java help us to write a file in fastest way?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3786
|
|
|
I'd stick with a BufferedWriter. It's actually designed to improve performance over a straight FileWriter (or similar) by buffering the write actions. For specialised purposes there might be something better, but for general purpose writing of text files it's a good choice.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
You do understand that there is a difference between writing a character stream and writing a binary file of bytes, right?
Bill
|
Java Resources at www.wbrogden.com
|
 |
Saral Saxena
Ranch Hand
Joined: Apr 22, 2011
Posts: 202
|
|
Hi Folks,
Is the concept of channel and Nio would also be applicable here...since I am also looking for the fastest way, I have been also doing lot of googling but in terms of performance I thnk channel would be a advantage,,!!
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4726
|
|
Bharath Raja wrote:Thanks for your reply. I have completly understood your point. However I am eager to know that which one of the i/o utilities in java help us to write a file in fastest way?
Why? Unless your answer is simply "because I'd like to know", it would appear that you haven't understood Fred's post at all, because what he's trying to tell you is that it is of little or no practical value to you.
However, for what it's worth, Matthew's answer is the one I would have given you too.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
William Brogden wrote:You do understand that there is a difference between writing a character stream and writing a binary file of bytes, right?
Bill
yes I do. It seems to be both are same in terms of performance. Am I right? Thanks.
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
Matthew Brown wrote:I'd stick with a BufferedWriter. It's actually designed to improve performance over a straight FileWriter (or similar) by buffering the write actions. For specialised purposes there might be something better, but for general purpose writing of text files it's a good choice.
Thanks for the informative explanation Matthew
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
Winston Gutkowski wrote:
Bharath Raja wrote:Thanks for your reply. I have completly understood your point. However I am eager to know that which one of the i/o utilities in java help us to write a file in fastest way?
Why? Unless your answer is simply "because I'd like to know", it would appear that you haven't understood Fred's post at all, because what he's trying to tell you is that it is of little or no practical value to you.
If I have interprted the Fred post wrongly, my apologies. Yes I meant that "because I'd like to know", since I need to pick the best IO utility to write the file.
Winston Gutkowski wrote:
However, for what it's worth, Matthew's answer is the one I would have given you too.
Winston
Thanks. I'm going with Matthew's suggestion.
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
Saral Saxena wrote:Hi Folks,
Is the concept of channel and Nio would also be applicable here...since I am also looking for the fastest way, I have been also doing lot of googling but in terms of performance I thnk channel would be a advantage,,!!
Thanks Dude. I will investigate on these too.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
Bharath Raja wrote:
William Brogden wrote:You do understand that there is a difference between writing a character stream and writing a binary file of bytes, right?
Bill
yes I do. It seems to be both are same in terms of performance. Am I right? Thanks.
Annoying buzzer noise
No, character stream output is expected to take more time because it may fiddle with character encoding on each individual character.
My (possibly out of date) Java Performance and Scalability vol 1 book by Dov Bulka shows a byte stream output taking half the time of a character stream.
Of course, handling a byte[] instead of buffering a stream of bytes is always going to be faster because it is closer to the actual operating system function. From java.io.FileOutputStream source code.
Bill
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4089
|
|
|
native methods...i am fascinated by what i read about that...not sure i would want to relearn c.
|
SCJP
|
 |
 |
|
|
subject: What is the fastest way to write a file?
|
|
|