Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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 ]
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic