Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Copying and moving files and directories

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Copying and moving files and directories

    I have been trying to learn how to copy and move files and directories and have tried many resources yet cannot seem to do it for some reason.
    This is the latest code that is not working.
    public class Serializer {
     
       public static void main(String[]args){
     
    	File aFile = new File("c:\\Users\\HASEEB\\agent.txt");
    	if(aFile.renameTo(new File("c:\\address\\" + aFile.getName()))){
    		System.out.println("File moved successfully");
    	}else{
    		System.out.println("File could not be moved");
    	}
    	}   
    }
    Here I'm trying to move agent.txt into the directory called address though keep getting file not moved.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Copying and moving files and directories

    From the API:

    public boolean renameTo(File dest)

    Renames the file denoted by this abstract pathname.

    Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

    Note that the Files class defines the move method to move or rename a file in a platform independent manner.

    Parameters:
    dest - The new abstract pathname for the named file
    Returns:
    true if and only if the renaming succeeded; false otherwise
    Throws:
    SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to either the old or new pathnames
    NullPointerException - If parameter dest is null
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Moving files between two folders
    By prakash24 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 26th, 2012, 10:50 AM
  2. Merging PDF Files using PDFBox in Sub/Directories
    By blivori in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 26th, 2011, 11:09 AM
  3. Moving files to a single list iteratively
    By Growler in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2010, 10:08 AM
  4. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  5. Moving files in a Directory
    By Sai in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 23rd, 2010, 04:49 AM