• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

java.io (File) - Why no copy?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know you can use streams to perform a file copy in Java, but why was the method "copy" not added to Java with the Merlin release (1.4)?
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I guess a line has to be drawn somewhere. If you want a copy method, you have to think about things like the destination directory, whether we should overwrite the file if it already exists, and so on. I guess before long you could have so many methods with so many different parameters that they might be better in a separate FileUtilities class instead.
 
Greg Ostravich
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

As far as destination directory that's not a problem - it isn't with the renameTo method. It accepts the destination as the only argument.

I just didn't understand why the File class had rename, delete, et. al. but not copy. I realize you have to draw the line somewhere, but why there?
Copy is very useful and I thought it was lame you have to implement your own copy everywhere because that's the one thing missing from File.

As far as over-writes they could pick one way or the other to do it and we could adjust. Worst case it doesn't over-write and you have to call the createNewFile method to do that before a copy method is called. Otherwise they could have decided to over-write and you have to see if the File exists first in case you want to preserve it using the exists method.

The reason I posted this question was that I was hoping somebody who knew of the debate (if there was one) over whether or not to include a copy functionality in the File class might shed some light on the decision.

I'm sure there must have been a good reason for that decision, but I just don't have a clue what that reason was.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess that the reason is that 'rename' and 'delete' correspond to individual UNIX system calls, while 'copy' does not. The File class's capabilities are a simple reflection of what's available in the UNIX filesystem API. You can implement 'copy' in terms of other available facilities, but 'rename' and 'delete' are primitive filesystem operations that have to be implemented in the kernel.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Copy is very useful and I thought it was lame you have to implement your own copy everywhere because that's the one thing missing from File.

You don't!

http://jakarta.apache.org/commons/io/
 
Greg Ostravich
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - that makes sense that it would be because of what mapped to the O/S the JVM ran on.

Thanks for the link to to the commons project.
I recently changed a kludgy roll-your-own debug to use Log4J and now I'll check out the Commons project to see what's there and start using some of that for what I need.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.io.File is quite platform-specific in a few spots - as it would be, given its nature. E.g. the javadocs of renameTo even mention that -depending on the platform- the method may succeed in different circumstances on different platforms.
File copying would have been tricky in the early days on Mac OS pre-X, where in addition to the data fork, there was also a resource fork, and an information stream. To this day, using OS X 'cp' on those files results in only the data fork being copied (i.e., file corruption). Which, incidentally, I suspect is also what the Commons IO package does, because there is no way to get at the other parts of the file using plain Java.
And since I'm reminiscing about pre-OS X days: it was perfectly OK to have two file systems with the same name, which would result in one (or both) of them being inaccessible in Java, since there was no notion of a 'root' directory (and java.io.File.listRoots came later).
[ August 07, 2005: Message edited by: Ulf Dittmer ]
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic