tags:

views:

251

answers:

4

Why does Java not have a file copy method? This seems like such an obvious thing to have, and it saves people from writing things like this example.

+7  A: 

My guess is because when the File io system was written, they decided they did not want to deal with the cross-platform problems of copying files, and punted - i.e. they said "this is doable by others, and is not that common".

One thing to keep in mind about Java is that it is cross-platform, so some things are more difficult because of that reality.

aperkins
"write once, debug everywhere"
MattC
Perhaps, but reading bytes from an input stream and writing them to an output stream is portable everywhere.
Loadmaster
+12  A: 

The Java API is missing more than just file copying. You might be interested in checking out the Apache Commons libraries. For example, the IO library's FileUtils provides file copying methods.

Thomas Owens
Yes, whenever I think to myself "Why is this feature not in Java?", invariably it is implemented by "the commons".
SingleShot
Which is fair enough - Java is a language, not a library. It's meant to make things *implementable*, not necessarily implement them in the JDK libraries.
Andrzej Doyle
It's a language and a library (java.util.*). Why can I write to a file, but not copy one?
C. Ross
You can write the code to copy a file, if you need to. My guess is that the developers of Java figured it was easy enough to do on your own, so they didn't write a method for it themselves.
Thomas Owens
@C. Ross: 'Cause what else are entry level CS classes supposed to use as quiz material?
Drew
@C Ross: Copying a file is a composition of lower level actions, reading, and writing. Writing to a file however is not a composition of lower level actions. If you can't write a file in java, you have no way to implement it using other parts of the system.
Jherico
+2  A: 

For the same reason Java does not have many other things. which end up being implemented by external libraries. I am sure you can easily find such a library, or you can write a function.

Omry
+6  A: 

java.io.File is a relatively simple class introduced in 1.0. JDK 1.0 didn't have much in it - mostly related to support for applets and the javac compiler. I guess there hasn't been a great deal of pressure to expand it - applets and enterprise software are not oriented in that direction.

However, lots has been added to I/O for JDK7. Including [java.nio.file.Path.copyTo][1].

[1]: http://download.java.net/jdk7/docs/api/java/nio/file/Path.html#copyTo%28java.nio.file.Path, java.nio.file.CopyOption...)

Tom Hawtin - tackline
Just looking through the Java 7 nio.files package, it looks like they added quite a bit.
Thomas Owens