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

Copying File  RSS feed

 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Most codes have something like


The buf have to be defined like new byte[1024]. How can I modify the above codes so that I don't really need to define 1024 bytes or something and just every byte the inputstream reads , every byte the outputstream writes until the file finishes ?
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java Tutorial Basic IO chapter. It contains many examples to demonstrate what functionality is available.
 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Found some old notes lying aroun in another external device during my course days and found what I needed... Thanks.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One correction for your code: don't check against > 0. If for whatever reason the read method returns 0, that is a perfectly valid return code, and it doesn't mean the end of the stream has been reached. That's what -1 means. So please change it in > -1, >= 0 or != -1 should you ever copy like this.
 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. Changed it to '-1' and works
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Tay]: How can I modify the above codes so that I don't really need to define 1024 bytes or something and just every byte the inputstream reads , every byte the outputstream writes until the file finishes ?

While the tutorial or other articles may indeed show you ways to do this, you might want to consider measuring how fast the other ways are. Usually you get better performance by preallocating an array for this sort of thing, which requires you to specify some number. I'm not sure why you want to avoid this, but I suspect it's not worth the trouble.

On the other hand, if you want to use java.nio.FileChannel, there's a way to do this with no preallocated array, an often the fastest possible transfer time (in Java at least). But NIO classes are generally more complex to use, so ask yourself: is it worth it?

[Rob]: If for whatever reason the read method returns 0, that is a perfectly valid return code, and it doesn't mean the end of the stream has been reached.

Well, according to the API, the only way you'll ever get a 0 return code is if the array you're reading into is size 0. Which is definitely not the case here. Still, I agree on general principle that it's better to use one of the other options you suggested - I always check against -1, because that most clearly matches what the API says.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!