Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying a File to other directory  RSS feed

 
Hari Shankar Gentyala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the requirement in which i have to copy dynamically determined files to other directories.I can do it by using the input and output streams but i am dealing with 5GB of data so i dont want the process to slow down.Can some one help me with this.
Thanks,
Hari
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With input and output streams you can read and write big chunks to & from byte arrays. The bigger the chunk, the fewer the IO calls to the OS and fewer the physical reads, and (one would expect) all the faster. It's still going to take time with gigabytes of data, tho. I haven't used NIO at all - there might be some non-blocking streams that would let you read and write at the same time on threads managed by NIO.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can use JDK 1.4, then java.nio.channels.FileChannel has transferTo() and transferFrom() methods that should be much faster, especially for large volumes of data. Otherwise, gotta use streams. And as Stan says, read a few big chunks, rather than lots of little chunks. Something like:

Also note that if you ever need to move a file rather than copy it, File has a renameTo() method that is probalbly what you want here.
[ December 02, 2003: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!