Results 1 to 6 of 6
  1. #1
    Hekmat is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Question [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

    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");
            }
    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?

    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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    Hekmat is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    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?

  4. #4
    whwillisiv is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Will work with any file of any sort ... byte by byte.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by whwillisiv View Post
    Will work with any file of any sort ... byte by byte.
    ++

    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.

  6. #6
    Hekmat is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Bean Shell
    By Bisweswar in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 12:36 AM
  2. How to create a Dialog Shell
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 07:52 PM
  3. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM
  4. how to issue the command of Ctrl-C (copy) in Java
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2007, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •