GeeCON Prague 2014*
The moose likes Java in General and the fly likes How do I copy a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I copy a file?" Watch "How do I copy a file?" New topic
Author

How do I copy a file?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm working on a project in which I need to copy various files. Basically, I have some copies in one location and I need to place copies of those files elsewhere but it's imperative that the original file remain in its place.
Using the java.io.File class, I have found that I can use renameTo in order to move a file from one location to another. This works great if I really wanted to MOVE the file, but I want the original to stay where it is and make a copy. Unfortunately, the only way that I have found to do this is to create a FileInputStream and a FileOutputStream and copy the contents of the file from source to destination byte by byte. Needless to say, this is inefficient - not to mention that, if I need to copy files over a network (which may very well occur), it's unbearably slow.
So does anyone know of a good way to copy a file?
Thanks,
Corey


SCJP Tipline, etc.
John Ipe
Greenhorn

Joined: Dec 29, 2003
Posts: 25
would it be an option for you to use Runtime.exec(). you could specify your OS's copy command in the parameter? and if the destination directory is a network drive that's mapped locally, it should still work?
hope i understood you right!
regards
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
For efficiency, you can use java.nio package for copy.
e172 of Java almanac 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by John Ipe:
would it be an option for you to use Runtime.exec(). you could specify your OS's copy command in the parameter

I believe that is a possibility, but how do I find out what the copy command is so that I can pass that as the parameter? I looked into the API Spec and I'm afraid that I found very little.
John Ipe
Greenhorn

Joined: Dec 29, 2003
Posts: 25
Originally posted by chi Lin:
For efficiency, you can use java.nio package for copy.
e172 of Java almanac 1.4
[/CODE]

very neat! Corey, I guess you'd want to go with this then?
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Hi

The Runtime.exec() is used to execute external programs. So there is no reason why you could not use it to call the copy command on your OS. But I would NOT recommend you do is. You code will soon end up messy as the parameters changed for different OS. First the copy program is different for windows and unix i.e "copy" and "cp". Also different windows platforms need different shells i.e. NT = cmd and 98 = command. If you do this you are losing one of the advantages of writing code that is platform independent.
I therefore recommend that you use channels to read and write the file in a new location. However if you have to write code that runs in 1.3 (This is something that I am forced to do at work) have a look are the old io package for an appropriate reader and a writer.
Chris.


SCJP 1.2, SCWCD, SCBCD
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
If you're sticking with the FileInputStream/FileOutputStream solution (I probably would), you should at least use a buffer instead of reading and writing byte-by-byte.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by chi Lin:
For efficiency, you can use java.nio package for copy.
e172 of Java almanac 1.4

I have to admit, I was amazed at just how well this works. I imagined this would still be too slow, but I am now able to move a hundred megs of data (or more) over our network in a matter of seconds using this technique. It is most definitely fast enough for what my application requires.
And, in addition, it doesn't connect to the underlying OS directly, so I don't lose my portability.
Thanks a ton.
marlon tan
Ranch Hand

Joined: Feb 18, 2001
Posts: 53
Originally posted by chi Lin:
For efficiency, you can use java.nio package for copy.
e172 of Java almanac 1.4


Hi,
I have a question regarding this method of copying file. dstFilename is very frequently read by another Java program. Is it possible that while executing dstChannel.transferFrom() method (while FileChannel is in the process of copying bytes of data), the other java program (simultaneously) reads dstFilename, will dstFilename be blank, or will the java program read half of what is supposed to be written to dstFilename?
Thank you very much
Regards,
Marlon
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
You could get a FileLock from the fileChannel, and that's intra-process. However, it's not always to be counted on. Some people create their own monitors. That is, your method might create a ".lock" file in the directory, then delete it when exiting.
Of course, if you found a ".lock" file, you'd be obligated to wait.
All best,
M


Java Regular Expressions
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The knuckleheads at Sun have been ignoring this problem since Java came out. Here is a bug that was submitted in 1997:
http://developer.java.sun.com/developer/bugParade/bugs/4032604.html
Makes me wonder what they are smoking at Sun if they think copying a file has different meanings (not implementations) on a Sun server as opposed to a Macintosh.
In my opinion, the developers at Sun are way too concerned about making simple things complicated (see the JSF specification) and not concerned enough about fixing serious holes in J2SE.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Just curious, what makes NIO channels significantly faster than older IOStreams or reader/writers?
- Manish
Ben Poole
Ranch Hand

Joined: Jul 17, 2003
Posts: 33
Add me to the list of those curious about this! If I get time I'll investigate... I assume it's essentially down to some buffering.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Just curious, what makes NIO channels significantly faster than older IOStreams or reader/writers?
They rely more heavily on native code, which often in turn can rely on your platform's hardware optimizations. The OS can tell the hard drive to copy from one spot on the disk to another; this may be accomplished without actually sending the data across the bus to the CPU or anywhere else. Traditional Java IO on the other hand requires that each and every byte of data be copied from the hard drive into a byte or byte[] in the JVM heap, and from there, copied back to the hard drive.
What I've described above for NIO is a best-case scenario, using the transferTo() or transferFrom() methods. Other NIO techniques rely on ByteBuffers instead, which will be a little slower than transferTo() because the bytes typically have to be transferred to RAM somewhere - but this is often still faster than IO classes because the RAM used by ByteBuffers is managed by the OS, not by the JVM, and so it's (probably) better optimized using native code for that platform.
The above statements are mostly from memory, and may well be mistaken in some aspects, so corrections welcome. A good resource is Java NIO from O'Reilly.
[ March 05, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ron Perrella
Greenhorn

Joined: May 26, 2004
Posts: 1
Originally posted by Thomas Paul:
The knuckleheads at Sun have been ignoring this problem since Java came out. Here is a bug that was submitted in 1997:

http://developer.java.sun.com/developer/bugParade/bugs/4032604.html

Makes me wonder what they are smoking at Sun if they think copying a file has different meanings (not implementations) on a Sun server as opposed to a Macintosh.

In my opinion, the developers at Sun are way too concerned about making simple things complicated (see the JSF specification) and not concerned enough about fixing serious holes in J2SE.


Oddly enough, I was one of the original complainers at the time. Clearly, sun is trying to develop multiplatform software while knowing only UNIX. UNIX has a very simple definition of files. Other platforms (indeed ALL OTHER platforms) have more sophisticated files. Resource Folks on Mac, Windows has additional streams of some kind, BeOS, MVS, VM/CMS, VAX/VMS,you name it. Even UNIX for God's stake! Hey Sun, ever heard of ACL? (hint: its in Solaris)?

Of course, they still haven't fixed it.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Ron Perrella:

UNIX has a very simple definition of files. Other platforms (indeed ALL OTHER platforms) have more sophisticated files. Resource Folks on Mac, Windows has additional streams of some kind, BeOS, MVS, VM/CMS, VAX/VMS,you name it.

Ron can you give some additional hints on sophisticated files?
I didn't mention any when I used windows last times, perhaps I'm missing something.

Linux has the sophisticated symbolic link(, and the somehow suspicious /dev /proc /sys filesystems).
Isn't a stream a very different thing than an file? Related, of course, deals with files, okay.


http://home.arcor.de/hirnstrom/bewerbung
Renee Doran
Greenhorn

Joined: Jun 30, 2004
Posts: 1
In the code snippet given in this topic the close of the channels happens in the try block. Should this be moved to a finally?
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
Originally posted by Renee Doran:
In the code snippet given in this topic the close of the channels happens in the try block. Should this be moved to a finally?


Give the guy a break! And what if the close methods throw an IOException? What then?!
Aurelien Faillon
Greenhorn

Joined: Jul 07, 2004
Posts: 9
I give you some code that work very well...

sorry but the comments are in french...



and you need so:



enjoy...


....A...............N<br />B.......T.......A....<br />............M........
 
GeeCON Prague 2014
 
subject: How do I copy a file?