Results 1 to 6 of 6
- 04-25-2009, 11:07 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
[Solved]Need some help with a copy command in a shell
Hi, I'm writing a simple Shell using Java for Windows for a uni course. So far things have been pretty straight forward using the java File class, I've made dir, cd, cd.. and a couple of other commands. Now on to the question:
I'm trying to make a copy command to copy a file and select a destination for it.
So far I've gotten by using the File class but I don't see anything in the class to copy a file. I tried using the boolean renameTo(String destination) command but it's not working (for some reason I haven't been able to figure out). I'd really appreciate some help with this
test returns false, and I can't figure out why. Can anyone think of another way for me to copy a file to a specific destination?Java Code:... File file1 = new File(currentPath); File file2 = new File("C:\\"); boolean [B]test[/B] = file1.renameTo(file2); if(!test) System.out.println("This file already exists and/or move operation failed"); }
Thanks for your time, hopefully someone can help me out!
Edit: Can't seem to edit the thread name to add a [solved] tag, is that a moderator thing or am I blind, heh.Last edited by Hekmat; 04-26-2009 at 11:03 PM.
- 04-26-2009, 12:32 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
As you've found renameTo() is for renaming a file, not copying it. And as the API says its behaviour is different on different platforms.
You're going to have to actually copy the file: read it using a stream based on one File instance and spit it out again using another stream based on another File instance.
Try using FileInputStream/FileOutputStream which are described in detail in Sun's Tutorial: Byte Streams. You might be able to do better than their example which reads one byte at a time.
- 04-26-2009, 10:37 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Thanks for the help. I did think of File(IO)Stream, but will that work for files that aren't text files? Like a .exe file for example?
- 04-26-2009, 11:48 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Will work with any file of any sort ... byte by byte.
- 04-26-2009, 10:23 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
++
OP: The next section in the Tutorial after "Byte Streams" is "Character Streams". They don't dwell on it but character streams (xxxReader/xxxWriter) are used where the bytes of a file represent characters in some (well known) way. For other sorts of file - that is for files like .exe where the bytes don't represent characters or where you don't know or don't care about that representation - use the byte streams: xxxInputStream/xxxOutputStream.
- 04-26-2009, 11:00 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Bean Shell
By Bisweswar in forum Advanced JavaReplies: 2Last Post: 08-16-2008, 12:36 AM -
How to create a Dialog Shell
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 07:52 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM -
how to issue the command of Ctrl-C (copy) in Java
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-18-2007, 03:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks