• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to file is slow

 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing some data to a file and this writing is done by a method called write(Object obj, OutputStream os). All this method does is query the object and write certain parts of the object into the stream. write(Object obj, OutputStream os) method constructs BufferedOutputStream and uses it.

I am calling this write() from two different places and it takes much longer when I call from one place. When I call from one part of our application it takes around 0.3 seconds but when I call the same method from a different part, it takes around 240 seconds. I couldn't understand why there is such a big difference. Both places construct output stream like this to pass: os = new FileOutputStream(new File(fileName)); and I pass the same filename and same object.

Any ideas on why it would take much longer when I call the same write method from a different part of the application? Thanks.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are writing substantially different objects, they may take different amounts of time to write.
The serialization mechanism caches the objects it reads and writes, so if you are writing the same object and the slow write is the first one, that's your reason.
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

I'm writing the exact same object.

Also, I'm not using serialization. I'm writing manually to the stream.

Any ideas?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raja Kannappan:

Also, I'm not using serialization. I'm writing manually to the stream.


Sorry about that. I assumed.
Is the destination file on a network-mapped drive or is it local storage?
Are you using the same file name for both writes? Since the second write would overwrite (or append, depending) the original, there may be some delay setting up the destination. Try writing to different files and see if that solves it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!