• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using streams

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys :

I usually write files in java by creating a byte[], and then using a bytearrayoutputstream to create the file.

Im thinking this is bad because for large files (files larger than the JVM memory allocation) this would cause an error.

My question is : What are the best practices for copying a file of considerable size (> than the JVM memory size) to disk using java ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually write files in java by creating a byte[], and then using a bytearrayoutputstream to create the file.


I don't follow this bit. I usually write to a ByteArrayOutputStream to capture what I'm writing into a byte array for some later use. Can you show a code snippet of what you're doing now?
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure



It seems the most intuitive way to do it for me...but Im
sure it breaks when youre file is larger than
the jvm memory.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an alternative, you can read and write a smaller chunk at a time. Set your buffer size to 1 or whatever memory will hold.



If you have to work on the data rather than just copy it, things get harder. Is that the case?
[ October 06, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!