• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Copying BIG file

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've been trying to copy a big file from one folder to another, and i got memory problems.
I finally decided to read part of the file and then write it to the destination: basically reading some, writting some, reading some , writting some.
Unfortunately, the destination file is bigger than the original file, and it seems, that it's copying more than it should.

This is the code:




Do you have an idea of what i may be doing wrong??

[Nitesh: Added code tags. Please use code tags]
 
Bartender
Posts: 9612
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to copy a file, you are doing way more work than you need to.
Go to example depot and search for "copy file" for some examples. Bookmark that site for later use!
 
relli Toto
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just found what i did wrong:



I forgot to empty the buffer right after setting the stringbuffer to 0!!!
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

relli Toto wrote:Just found what i did wrong:



Well, kinda. I don't really understand the intermediate step of appending to a StringBuffer anyway, though. Because of this, your method will only work for text files, and even then, only for your platform's default encoding, because you haven't accounted for locale when converting from byte[] to String. Also, this is still quite slow and quite memory intensive. A good file-copy routine looks something like

 
relli Toto
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true, it looks better the way you did it, and it seems to work too!!
I actually had a memory problem before that's why i went all this way with this long ugly code!

Many thanks mate!
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic