• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Using streams

 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic