This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is the fastest way to write a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the fastest way to write a file?" Watch "What is the fastest way to write a file?" New topic
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: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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: 4342
    
    7

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: 12759
    
    5
You do understand that there is a difference between writing a character stream and writing a binary file of bytes, right?

Bill
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: 7518
    
  18

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?
Articles by Winston can be found here
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: 12759
    
    5
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: 4340
    
    2

native methods...i am fascinated by what i read about that...not sure i would want to relearn c.


SCJP
Visit my download page
 
Don't get me started about those stupid light bulbs.
 
subject: What is the fastest way to write a file?
 
Similar Threads
reading / writing windows registry entries...
how to quickly write a long string to a file ?
Collections
How do I check if a String is an int
What is the best way to work with a large amount of data?