This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Copy a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Copy a file" Watch "Copy a file" New topic
Author

Copy a file

Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I was wondering if there was a way to copy a file, something provided by the API.

I looked for this on the web, and the only way I found was actually reading all bytes from one file and writing it to another one. If thats the way to do it in Java, it sucks

Thanks,
Alex
[ May 02, 2005: Message edited by: Alex Turcot ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
That's the only way I know of. However, you can get a little more sophisticated by using a BufferedInputStream and BufferedOutputStream or a BufferedReader and BufferedWriter. These allow you to read and write a line at a time instead of just a byte at a time. Either way, you will have to write a short loop to iterate over the whole input file.

Layne


Java API Documentation
The Java Tutorial
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Thanks. Don't you think it sucks? I feel like calling sun to tell them to fix it. Maybe it is in 1.5 ...

I don't understand how this can be.

I looove java and I'm often debating with my friend who does VB.net...
I lost my last argument with him because of this. I was speechless

Thanks again,
Alex
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Move or Copy?

For move you can just use 'File.renameTo(File dest)'.

For Copy, because you are duplicating all the data, I believe you do have to read from one to the other, although I don't see how it 'sucks', it only takes a few lines of code.
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I agree it might just be a few lines of code...

I guess it can be scary sometime.. If you want to move a database file, or a .exe, its just odd (to me) to copy it byte by byte.

I know, thats what would happen behind the scene anyway, but I don't like the fact that you have to re-write this every time. And test it.

Alex
[ May 02, 2005: Message edited by: Alex Turcot ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You can do something like this with the nio package


I really don't see all that much to be worried about. Nothing to really test.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Originally posted by Alex Turcot:
Hi,
I know, thats what would happen behind the scene anyway, but I don't like the fact that you have to re-write this every time. And test it.
[ May 02, 2005: Message edited by: Alex Turcot ]


Write it once, test it once, use it every time.

Don't forget that code can be reused...


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
There is also the jakarta commons io package that, among other things, has an IOUtils class with several copy methods, if you don't want to write it yourself.

Ask your VB.net friend to write a client server app that copies files from a local WinXP machine to a remote Linux server.

I got tired of FTPing files to the same spot for backup one or two at a time. I wrote two java classes and a .bat file. I can drag n' drop a file onto the .bat file and it gets pushed to my Linux box for backup.

Then there is my favorite VB story, happened to me with VB6. I wrote an app, compiled and ran, but it was failing and I couldn't figure out why. After much frustration I cut the offending line and pasted it back in place, recompiled and it worked. I've never coded in VB out of choice again.
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516


I really don't see all that much to be worried about. Nothing to really test.[/qb]<hr></blockquote>


Ahh, this is very close to what I was looking for !
[ May 02, 2005: Message edited by: Alex Turcot ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Alex Turcot:
Hi,

I agree it might just be a few lines of code...

I guess it can be scary sometime.. If you want to move a database file, or a .exe, its just odd (to me) to copy it byte by byte.

I know, thats what would happen behind the scene anyway, but I don't like the fact that you have to re-write this every time. And test it.

Alex

[ May 02, 2005: Message edited by: Alex Turcot ]


You don't have to rewrite it every time you need to use it! That's near hypocracy in the OO world. Just write the method in a utility class that you can access from every program that needs it. Classes are the epitomy of code reuse IMO.

Layne
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I know, you are right, I just said this to illustrate my point.
I agree I could write it once and re-use it.

I just think that this is a basic functionality and that it should be part of the API.
But, now that I discovered the FileChannel, I am happy, I feel this is what I wanted. So after all, it is in the API, even if its not called copyFile().

Thanks for the help,
Alex
[ May 02, 2005: Message edited by: Alex Turcot ]
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
Originally posted by Steven Bell:
I got tired of FTPing files to the same spot for backup one or two at a time. I wrote two java classes and a .bat file. I can drag n' drop a file onto the .bat file and it gets pushed to my Linux box for backup.


you want rsync. or, better yet, unison. that way only the modified parts of the files will be transferred over, the data will be automagically compressed while in transit, and you can do entire directory trees at once, recursively.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I've used rsync for other things, but this was just a single use case very specific app for my use only. The less code to make it work the better.

As it is the client and server have about 15-20 lines of code each. One line in the .bat file just so I could do drag n' drop. It's something like

java -jar transfer.jar $1

It's been awhile so I'd have to go back and look.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy a file