Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the fastest way to write a file?

 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4369
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
native methods...i am fascinated by what i read about that...not sure i would want to relearn c.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic