Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying BIG file  RSS feed

 
relli Toto
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]
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X 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!!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!