Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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!
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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.
 
Wanderer
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.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic