Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a better/faster/more efficient way to copy files?  RSS feed

 
Teddy Wang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got code that I've been using since JDK 1.1. It's basically the same code that you will readily find if you search for "java file copy" on Google which uses File and FileInputStream/FileOutputStream. This can't possibly be the best/fastest/more efficient way to copy files. Does anybody have another way to do this? I've got an app that needs to copy thousands of files (of varying sizes) as quickly as possible.

Thanks!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Teddy Wang:
Does anybody have another way to do this? I've got an app that needs to copy thousands of files (of varying sizes) as quickly as possible.

Thanks!


I havent tried it myself but you can try making a system call, sort of bulk copy command.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since, JDK 1.4, the java.nio packages allow to copy much, much faster (at least, much faster on some operating systems). Try this:

Yes, that's ugly, but it works. FileChannels can be quite fast, especially when they don't have to transfer any of their data into or out of the JVM.

If you're trying to maximize total speed for many transfers, you may also benefit from having multiple threads do the work. This depends a lot on the nature of your filesystem. I would simply try it and see how fast it is. Try varying the number of threads, too. You may get the most benefit from having more than one thread, but not hundreds. In JDK 5+, the java.util.concurrent.Executors class makes it easy to create a thread pool for this sort of thing.

Having said all that, it's also possible that the best performance you get will be from a system command, as Maneesh suggests. Or it may be the same as you get with a FileChannel. Try both and see. Both will almost certainly be better than the old FileInputStream / FileOutputStream solution though. Please let us know which works best for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!