Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File copying in Java

What is a better option (in terms of performance): copying a file using fileinputstream and fileoutputstream or running a OS specific command copy command from Java?

like image 289
jjoshi Avatar asked Apr 28 '26 04:04

jjoshi


2 Answers

I'm quite sure using the OS specific copy command would be faster or at least as fast as a simple self-written solution. The OS specific command probably uses a sensible buffer size and other optimizations which you would otherwise have to figure out yourself.

Edit:
x-x is right, you should not call the copy command directly. I thought Java already had a copy method, like File.copy() or something, but I couldn't find anything, not even in JDIC. So Apache Commons IO is probably the way to go.

like image 132
mooware Avatar answered Apr 29 '26 19:04

mooware


Do it in Java.

One problem with running an OS command is that you have to create a full process at the OS kernel level and that's a heavyweight operation. There is a large fixed overhead which will be particularly serious for smaller files.

The other problem is that it adds a system dependency without a good reason.

like image 23
DigitalRoss Avatar answered Apr 29 '26 19:04

DigitalRoss